Custom entity erstellen mit dem Modul Inline Entity Form
am 04.09.2014 - 21:28 Uhr in
Hallo an alle,
ich beschäftige mich seit gestern mit dem Kreieren von custom entities, hartes Brot ehrlich gesagt.
Was ich nicht verstehe ist die Implementierung von Inline Entity Form, speziell dabei wie ich den oder die Controller einbaue.
Das Ziel ist es, eine entity mit vielen Spalten zu bauen, also nicht fieldable, der Performance wegen, und dann per entity reference mit inline entity form in eine node zu laden.
Also:
In der Readme von Inline Entity Form steht folgendes zu dem Thema:
Integrating with Inline Entity Form
-----------------------------------
An entity type can add support for this module by declaring the
inline entity form controller class in its entity info:
$entity_info['commerce_line_item']['inline entity form'] = array(
'controller' => 'CommerceLineItemInlineEntityFormController',
);
The controller needs to extend EntityInlineEntityFormController and at least
override entityForm() to provide a function entity form.
Meine Test Entity hat bisher folgendes Aussehen, wobei das ehrlich gesagt copy-paste ist:
function demo_entity_info() {
$info = array();
$info['project'] = array(
'label' => t('Project'),
'base table' => 'demo_projects',
'entity keys' => array(
'id' => 'id',
'label' => 'name',
),
'entity class' => 'ProjectEntity',
'controller class' => 'ProjectEntityController',
'access callback' => 'demo_access_callback',
'uri callback' => 'entity_class_uri',
'admin ui' => array(
'path' => 'admin/projects',
'controller class' => 'EntityDefaultUIController',
),
'views controller class' => 'EntityDefaultViewsController',
'module' => 'demo',
);
return $info;
}
d.h. ich greife auf eine via hook_schema erstellte DB Tabelle namens demo_projects zu, erweitere die Klasse Entity des Systems mit ProjectEntity, so dass die URL zur Ansicht des jeweiligen Entitäteninhaltes verândert wird, erweitere die Klasse EntityApiController, die ich vom Modul Entity geschenkt bekommen habe, mittels ProjectEntityController um die Ansicht der Inhalte entsprechend anzupassen bzw. zu erweitern, und habe für die admin UI bzw für views jeweils 1 Controller.
Habe natürlich auch in den issues vom modul nach Erklärungen gesucht.
Da steht wie folgt:
Schau in der include vom Modul wie das für nodes gemacht ist, und hau in die hook_info folgendes rein:
<?php
$entity_info['inline entity form'] = array(
'controller' => 'YourEntityInlineEntityFormController',
);
?>
Meine Frage somit: mit was erweitere ich demnach diese Klasse InlineEntityFormController, die Erweiterung hiesse ja dann ProjectInlineEntitwFormController.
Aber was schreibe ich in die extension? hat das schon jemand mal gemacht???
Danke vorab, hier mein demo Code komplett:
<?php
/**
* Implements hook_entity_info().
*/
function demo_entity_info() {
$info = array();
$info['project'] = array(
'label' => t('Project'),
'base table' => 'demo_projects',
'entity keys' => array(
'id' => 'id',
'label' => 'name',
),
'entity class' => 'ProjectEntity',
'controller class' => 'ProjectEntityController',
'access callback' => 'demo_access_callback',
'uri callback' => 'entity_class_uri',
'admin ui' => array(
'path' => 'admin/projects',
'controller class' => 'EntityDefaultUIController',
),
'fieldable' => TRUE,
'bundles' => array(
'project' => array(
'label' => t('Project'),
'admin' => array(
'path' => 'admin/projects',
),
),
),
$entity_info['inline entity form'] = array(
'controller' => 'ProjectInlineEntityFormController',
),
'views controller class' => 'EntityDefaultViewsController',
'module' => 'demo',
);
return $info;
}
/**
* Implements hook_menu()
*/
function demo_menu() {
$items = array();
$items['projects'] = array(
'title' => 'Our projects demo',
'page callback' => 'demo_projects',
'access arguments' => array('access content'),
);
$items['project/%'] = array(
'title' => 'Project',
'page callback' => 'demo_view_project',
'page arguments' => array(1),
'access arguments' => array('access content'),
);
return $items;
}
/**
* Access callback for project entities.
*/
function demo_access_callback($op, $project = NULL, $account = NULL) {
if ($op == 'view' || $op == 'update' || $op == 'create' || $op == 'delete') {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Callback function for our project entities test path
*/
function demo_projects() {
$projects = entity_load('project', array(1, 2, 3));
// Saving new entities
if (!isset($projects[3])) {
$entity = entity_create('project', array('id' => 3));
$entity->name = t('Spring House');
$entity->description = t('Some more lipsum.');
$entity->deadline = '1397501132';
$entity->save();
}
// Listing entities
$list = entity_view('project', $projects);
$output = array();
foreach ($list['project'] as $project) {
$output[] = drupal_render($project);
}
return implode($output);
}
/**
* Callback function for displaying the individual project page
*/
function demo_view_project($id) {
$projects = entity_load('project', array($id));
$project = $projects[$id];
drupal_set_title($project->name);
$output = entity_view('project', array($project));
return $output;
}
/**
* Form definition for adding / editing a project.
*/
function project_form($form, &$form_state, $project = NULL) {
$form['name'] = array(
'#title' => t('Project name'),
'#type' => 'textfield',
'#default_value' => isset($project->name) ? $project->name : '',
'#required' => TRUE,
);
$form['description'] = array(
'#title' => t('Project description'),
'#type' => 'textarea',
'#default_value' => isset($project->description) ? $project->description : '',
'#required' => TRUE,
);
$form['deadline'] = array(
'#title' => t('Project deadline'),
'#type' => 'textfield',
'#default_value' => isset($project->deadline) ? $project->deadline : '',
'#required' => TRUE,
);
field_attach_form('project', $project, $form, $form_state);
$form['submit'] = array(
'#type' => 'submit',
'#value' => isset($project->id) ? t('Update project') : t('Save project'),
'#weight' => 50,
);
return $form;
}
/**
* Submit handler for the project add/edit form.
*/
function project_form_submit($form, &$form_state) {
$project = entity_ui_form_submit_build_entity($form, $form_state);
$project->save();
drupal_set_message(t('The project: @name has been saved.', array('@name' => $project->name)));
$form_state['redirect'] = 'admin/projects';
}
/**
* Implements hook_entity_property_info().
*/
function demo_entity_property_info() {
$info = array();
$info['project']['properties']['id'] = array(
'label' => t('Project ID'),
'description' => t('The ID of the project.'),
'type' => 'integer',
'schema field' => 'id',
);
$info['project']['properties']['name'] = array(
'label' => t('Project name'),
'description' => t('Name of the project.'),
'type' => 'text',
'schema field' => 'name',
);
$info['project']['properties']['description'] = array(
'label' => t('Project description'),
'description' => t('Description of the project.'),
'type' => 'text',
'schema field' => 'description',
);
$info['project']['properties']['deadline'] = array(
'label' => t('Deadline'),
'description' => t('Project deadline.'),
'type' => 'date',
'schema field' => 'deadline',
);
return $info;
}
/**
* Extending the EntityAPIController for the Project entity.
*/
class ProjectEntityController extends EntityAPIController {
public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
$build = parent::buildContent($entity, $view_mode, $langcode, $content);
// Our additions to the $build render array.
$build['description'] = array(
'#type' => 'markup',
'#markup' => check_plain($entity->description),
'#prefix' => '<div class="project-description">',
'#suffix' => '</div>',
);
$build['deadline'] = array(
'#type' => 'markup',
'#markup' => date('d F, Y', check_plain($entity->deadline)),
'#prefix' => '<p>Deadline: ',
'#suffix' => '</p>',
);
return $build;
}
}
/**
* Project entity class extending the Entity class
*/
class ProjectEntity extends Entity {
/**
* Change the default URI from default/id to project/id
*/
protected function defaultUri() {
return array('path' => 'project/' . $this->identifier());
}
}
- Anmelden oder Registrieren um Kommentare zu schreiben
Neue Kommentare
vor 1 Tag 2 Stunden
vor 2 Tagen 31 Minuten
vor 3 Tagen 19 Stunden
vor 4 Tagen 1 Stunde
vor 4 Tagen 3 Stunden
vor 4 Tagen 15 Stunden
vor 4 Tagen 17 Stunden
vor 4 Tagen 17 Stunden
vor 4 Tagen 18 Stunden
vor 4 Tagen 18 Stunden