ConfigurableEntityReferenceItem.php 6.21 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\entity_reference\ConfigurableEntityReferenceItem.
6 7
 */

8
namespace Drupal\entity_reference;
9 10 11 12 13 14

use Drupal\field\Plugin\Type\FieldType\ConfigEntityReferenceItemBase;
use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemInterface;
use Drupal\field\FieldInterface;

/**
15
 * Alternative plugin implementation of the 'entity_reference' field type.
16
 *
17 18
 * Replaces the Core 'entity_reference' entity field type implementation, this
 * supports configurable fields, auto-creation of referenced entities and more.
19
 *
20
 * @see entity_reference_field_info_alter().
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 *
 */
class ConfigurableEntityReferenceItem extends ConfigEntityReferenceItemBase implements ConfigFieldItemInterface {

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldInterface $field) {
    $schema = array(
      'columns' => array(
        'target_id' => array(
          'description' => 'The ID of the target entity.',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
        'revision_id' => array(
          'description' => 'The revision ID of the target entity.',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => FALSE,
        ),
      ),
      'indexes' => array(
        'target_id' => array('target_id'),
      ),
    );

    // Create a foreign key to the target entity type base type.
50
    $entity_manager = \Drupal::service('entity.manager');
51
    $target_type = $field->getFieldSetting('target_type');
52
    if (is_subclass_of($entity_manager->getControllerClass($target_type, 'storage'), 'Drupal\Core\Entity\FieldableDatabaseStorageController')) {
53
      $entity_info = $entity_manager->getDefinition($target_type);
54 55 56
      $base_table = $entity_info['base_table'];
      $schema['foreign keys'][$base_table] = array(
        'table' => $base_table,
57
        'columns' => array('target_id' => $entity_info['entity_keys']['id']),
58 59 60 61 62 63 64 65 66 67 68 69 70
      );
    }

    return $schema;
  }

  /**
   * {@inheritdoc}
   */
  public function preSave() {
    $entity = $this->get('entity')->getValue();
    $target_id = $this->get('target_id')->getValue();

71
    if (!$target_id && !empty($entity) && $entity->isNew()) {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
      $entity->save();
      $this->set('target_id', $entity->id());
    }
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, array &$form_state, $has_data) {
    // Select the target entity type.
    $entity_type_options = array();
    foreach (\Drupal::entityManager()->getDefinitions() as $entity_type => $entity_info) {
      // @todo As the database schema can currently only store numeric IDs of
      // referenced entities and configuration entities have string IDs, prevent
      // configuration entities from being referenced.
      if (!is_subclass_of($entity_info['class'], '\Drupal\Core\Config\Entity\ConfigEntityInterface')) {
        $entity_type_options[$entity_type] = $entity_info['label'];
      }
    }

    $element['target_type'] = array(
      '#type' => 'select',
      '#title' => t('Type of item to reference'),
      '#options' => $entity_type_options,
      '#default_value' => $this->getFieldSetting('target_type'),
      '#required' => TRUE,
      '#disabled' => $has_data,
      '#size' => 1,
    );

    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function instanceSettingsForm(array $form, array &$form_state) {
109
    $instance = $form_state['instance'];
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

    // Get all selection plugins for this entity type.
    $selection_plugins = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionGroups($this->getFieldSetting('target_type'));
    $handler_groups = array_keys($selection_plugins);

    $handlers = \Drupal::service('plugin.manager.entity_reference.selection')->getDefinitions();
    $handlers_options = array();
    foreach ($handlers as $plugin_id => $plugin) {
      // We only display base plugins (e.g. 'default', 'views', ...) and not
      // entity type specific plugins (e.g. 'default_node', 'default_user',
      // ...).
      if (in_array($plugin_id, $handler_groups)) {
        $handlers_options[$plugin_id] = check_plain($plugin['label']);
      }
    }

    $form = array(
      '#type' => 'container',
      '#attached' => array(
        'css' => array(drupal_get_path('module', 'entity_reference') . '/css/entity_reference.admin.css'),
      ),
      '#process' => array(
        '_entity_reference_field_instance_settings_ajax_process',
      ),
      '#element_validate' => array(array(get_class($this), 'instanceSettingsFormValidate')),
    );
    $form['handler'] = array(
      '#type' => 'details',
      '#title' => t('Reference type'),
      '#tree' => TRUE,
      '#process' => array('_entity_reference_form_process_merge_parent'),
    );

    $form['handler']['handler'] = array(
      '#type' => 'select',
      '#title' => t('Reference method'),
      '#options' => $handlers_options,
147
      '#default_value' => $instance->getFieldSetting('handler'),
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
      '#required' => TRUE,
      '#ajax' => TRUE,
      '#limit_validation_errors' => array(),
    );
    $form['handler']['handler_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Change handler'),
      '#limit_validation_errors' => array(),
      '#attributes' => array(
        'class' => array('js-hide'),
      ),
      '#submit' => array('entity_reference_settings_ajax_submit'),
    );

    $form['handler']['handler_settings'] = array(
      '#type' => 'container',
      '#attributes' => array('class' => array('entity_reference-settings')),
    );

167 168
    $handler = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($instance);
    $form['handler']['handler_settings'] += $handler->settingsForm($instance);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

    return $form;
  }

  /**
   * Form element validation handler; Stores the new values in the form state.
   *
   * @param array $form
   *   The form where the settings form is being included in.
   * @param array $form_state
   *   The form state of the (entire) configuration form.
   */
  public static function instanceSettingsFormValidate($form, &$form_state) {
    if (isset($form_state['values']['instance'])) {
      unset($form_state['values']['instance']['settings']['handler_submit']);
184
      $form_state['instance']->settings = $form_state['values']['instance']['settings'];
185 186 187 188
    }
  }

}