Startseite
  • » Home
  • » Handbuch & FAQ
  • » Forum
  • » Übersetzungsserver
  • » Suche
Startseite › Forum › Drupalcenter.de › Module ›

Custom entity erstellen mit dem Modul Inline Entity Form

Eingetragen von maen (547)
am 04.09.2014 - 21:28 Uhr in
  • Module
  • Drupal 7.x

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:

Zitat:

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());
  }
 
}

‹ Redirect nach Login auf aktueller Seite Views Ajax und Refresh-Problem ›
  • Anmelden oder Registrieren um Kommentare zu schreiben

Benutzeranmeldung

  • Registrieren
  • Neues Passwort anfordern

Aktive Forenthemen

  • für drupal11 ein Slider Modul
  • [gelöst] W3CSS Paragraphs Views
  • Drupal 11 neu aufsetzen und Bereiche aus 10 importieren
  • Wie erlaubt man neuen Benutzern auf die Resetseite zugreifen zu dürfen.
  • [gelöst] Anzeigeformat Text mit Bild in einem Artikel, Drupal 11
  • Social Media Buttons um Insteragram erweitern
  • Nach Installation der neuesten D10-Version kein Zugriff auf Website
  • Composer nach Umzug
  • [gelöst] Taxonomie Begriffe zeigt nicht alle Nodes an
  • Drupal 11 + Experience Builder (Canvas) + Layout Builder
  • Welche KI verwendet ihr?
  • Update Manger läst sich nicht Installieren
Weiter

Neue Kommentare

  • melde mich mal wieder, da ich
    vor 2 Wochen 2 Tagen
  • Hey danke
    vor 2 Wochen 3 Tagen
  • Update: jetzt gibt's ein
    vor 2 Wochen 4 Tagen
  • Hallo, im Prinzip habe ich
    vor 3 Wochen 1 Tag
  • Da scheint die Terminologie
    vor 3 Wochen 1 Tag
  • Kannst doch auch alles direkt
    vor 3 Wochen 6 Tagen
  • In der entsprechenden View
    vor 3 Wochen 6 Tagen
  • Dazu müsstest Du vermutlich
    vor 3 Wochen 6 Tagen
  • gelöst
    vor 6 Wochen 2 Tagen
  • Ja natürlich. Dass ist etwas,
    vor 6 Wochen 3 Tagen

Statistik

Beiträge im Forum: 250233
Registrierte User: 20455

Neue User:

  • ByteScrapers
  • Mroppoofpaync
  • 4aficiona2

» Alle User anzeigen

User nach Punkten sortiert:
wla9461
stBorchert6003
quiptime4972
Tobias Bähr4019
bv3924
ronald3857
md3717
Thoor3678
Alexander Langer3416
Exterior2903
» User nach Punkten
Zur Zeit sind 0 User und 20 Gäste online.

Hauptmenü

  • » Home
  • » Handbuch & FAQ
  • » Forum
  • » Übersetzungsserver
  • » Suche

Quicklinks I

  • Infos
  • Drupal Showcase
  • Installation
  • Update
  • Forum
  • Team
  • Verhaltensregeln

Quicklinks II

  • Drupal Jobs
  • FAQ
  • Drupal-Kochbuch
  • Best Practice - Drupal Sites - Guidelines
  • Drupal How To's

Quicklinks III

  • Tipps & Tricks
  • Drupal Theme System
  • Theme Handbuch
  • Leitfaden zur Entwicklung von Modulen

RSS & Twitter

  • Drupal Planet deutsch
  • RSS Feed News
  • RSS Feed Planet
  • Twitter Drupalcenter
Drupalcenter Team | Impressum & Datenschutz | Kontakt
Angetrieben von Drupal | Drupal is a registered trademark of Dries Buytaert.
Drupal Initiative - Drupal Association