Hướng dẫn custom entity reference: Drupal & Entity reference

Hướng dẫn custom entity reference: Drupal & Entity reference

There are some good examples out there on how to create a custom Entity reference Behaviour plugin, but not so much for Selection handlers.

Why would you want to do this? A number of reasons. For me in Harmony Forum it was because the base Entity reference selection handler wasn't returning correct results on an autocomplete field and I've got custom access rules to consider, and due to future expansion it makes sense. An alternative to making our own would be to use Views, but this idea doesn't appeal due to the performance overhead compared to EntityField Query.

Behaviour handler examples can be found in the Entity reference module itself, Entityreference Prepopulate and Organic Groups.

Before we jump in, if the base generic Entity reference selection handler does most of what you want, and you only have a custom entity to integrate, all you need to do is name your class in a particular way and it'll be automatically used.

Let's have a look in entityreference/plugins/selection/EntityReference_SelectionHandler_Generic.class.php

class EntityReference_SelectionHandler_Generic implements EntityReference_SelectionHandler {

  /**
   * Implements EntityReferenceHandler::getInstance().
   */
  public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
    $target_entity_type = $field['settings']['target_type'];

    // Check if the entity type does exist and has a base table.
    $entity_info = entity_get_info($target_entity_type);
    if (empty($entity_info['base table'])) {
      return EntityReference_SelectionHandler_Broken::getInstance($field, $instance);
    }

if (class_exists($class_name = 'EntityReference_SelectionHandler_Generic_' . $target_entity_type)) {
      return new $class_name($field, $instance, $entity_type, $entity);
    }
    else {
      return new EntityReference_SelectionHandler_Generic($field, $instance, $entity_type, $entity);
    }
  }
...

Note the if (class_exists($class_name = 'EntityReference_SelectionHandler_Generic_' . $target_entity_type)) {chunk, this is your in if you don't want your own totally custom selection handler.

Right, still want to do this? Great!

We'll work with the assumption that this will be the structure of our module when we're done:

example.info
example.module
plugins/entityreference/selection/example_selection_handler.inc
plugins/entityreference/selection/ExampleSelectionHandler.class.php

We'll be creating "ExampleSelectionHandler" which extends "EntityReference_SelectionHandler_Generic".

First things first, add "plugins/entityreference/selection/ExampleSelectionHandler.class.php" to "files[]" in the module .info so it can be autoloaded (though I don't believe it's necessary).

files[] = plugins/entityreference/selection/ExampleSelectionHandler.class.php

Next in your module (for our purposes "Example") implement hook_ctools_plugin_directory to point to where your plugins are.

/**
* Implements hook_ctools_plugin_directory().
*/
function example_ctools_plugin_directory($module, $plugin) {
  if ($module == 'entityreference') {
    return 'plugins/entityreference/' . $plugin;
  }
}

Now we need to create an unusually basic definition file for the Selection handler "example_selection_handler.inc". All this file needs is an array with two items:

<?php

$plugin = array(
  'title' => t(‘Example Selection handler'),
  'class' => 'ExampleSelectionHandler',
);

Finally we define the class file which should be named as per the value of $plugin[‘class'] with ".class.php" appended. The best example to look at is probably the "base" or default Selection handler within Entity reference which can be found at "entityreference/plugins/selection/EntityReference_SelectionHandler_Generic.class.php".

What should go in your implementation depends on what you want to do, the only requirement of your custom Selection handler is that it has the method "getInstance". Here's an example.

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Mercury LoadRunner 8.0

Các tính năng của Mercury LoadRunner 8.0

Công cụ Virtual User Generator để tạo User ảo (Virtual User), với các hành vi được copy từ thao tác của người duyệt web.

Những tips cần thiết đọc đề làm việc với Drupal 8

Những tips cần thiết đọc đề làm việc với Drupal 8

One useful thing during my D8 learning that I came across is that you can find out whether a certain function has 'gone away' or has been replaced by something else in D8.

Surface Pro của Microsoft lần đầu cháy hàng

Surface Pro của Microsoft lần đầu cháy hàng

Với cấu hình vượt trội so với phiên bản anh em Surface RT, Surface Pro đã có một khởi đầu suôn sẻ hơn khi bán hết veo trên trang MicrosoftStore.com ngay trong buổi sáng lên kệ đầu tiên.

Công ty diệt chuột T&C

 

Diet con trung