TaxonomyTermReferenceItem.php 4.51 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\taxonomy\Plugin\field\field_type\TaxonomyTermReferenceItem.
 */

namespace Drupal\taxonomy\Plugin\Field\FieldType;

10
use Drupal\Core\Field\FieldStorageDefinitionInterface;
11
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
12
use Drupal\Core\Form\FormStateInterface;
13
use Drupal\Core\Form\OptGroup;
14
use Drupal\Core\Session\AccountInterface;
15
use Drupal\Core\TypedData\OptionsProviderInterface;
16 17 18 19 20 21 22 23 24 25

/**
 * Plugin implementation of the 'term_reference' field type.
 *
 * @FieldType(
 *   id = "taxonomy_term_reference",
 *   label = @Translation("Term Reference"),
 *   description = @Translation("This field stores a reference to a taxonomy term."),
 *   default_widget = "options_select",
 *   default_formatter = "taxonomy_term_reference_link",
26 27
 *   list_class = "\Drupal\Core\Field\EntityReferenceFieldItemList",
 *   constraints = {"ValidReference" = {}}
28 29
 * )
 */
30
class TaxonomyTermReferenceItem extends EntityReferenceItem implements OptionsProviderInterface {
31

32 33 34
  /**
   * {@inheritdoc}
   */
35
  public static function defaultStorageSettings() {
36 37 38 39 40 41 42 43 44
    return array(
      'target_type' => 'taxonomy_term',
      'options_list_callback' => NULL,
      'allowed_values' => array(
        array(
          'vocabulary' => '',
          'parent' => 0,
        ),
      ),
45
    ) + parent::defaultStorageSettings();
46 47
  }

48 49 50 51 52 53
  /**
   * {@inheritdoc}
   */
  public function getPossibleValues(AccountInterface $account = NULL) {
    // Flatten options firstly, because Possible Options may contain group
    // arrays.
54
    $flatten_options = OptGroup::flattenOptions($this->getPossibleOptions($account));
55 56 57 58 59 60 61 62 63 64 65 66 67 68
    return array_keys($flatten_options);
  }

  /**
   * {@inheritdoc}
   */
  public function getPossibleOptions(AccountInterface $account = NULL) {
    return $this->getSettableOptions($account);
  }

  /**
   * {@inheritdoc}
   */
  public function getSettableValues(AccountInterface $account = NULL) {
69 70
    // Flatten options firstly, because Settable Options may contain group
    // arrays.
71 72 73
    $values = array_keys(OptGroup::flattenOptions($this->getSettableOptions($account)));
    $values[] = static::$NEW_ENTITY_MARKER;
    return $values;
74 75 76 77 78 79
  }

  /**
   * {@inheritdoc}
   */
  public function getSettableOptions(AccountInterface $account = NULL) {
80
    if ($callback = $this->getSetting('options_list_callback')) {
81 82 83 84
      return call_user_func_array($callback, array($this->getFieldDefinition(), $this->getEntity()));
    }
    else {
      $options = array();
85
      foreach ($this->getSetting('allowed_values') as $tree) {
86 87 88
        if ($vocabulary = entity_load('taxonomy_vocabulary', $tree['vocabulary'])) {
          if ($terms = taxonomy_get_tree($vocabulary->id(), $tree['parent'], NULL, TRUE)) {
            foreach ($terms as $term) {
89
              $options[$term->id()] = str_repeat('-', $term->depth) . $term->getName();
90 91 92 93 94 95
            }
          }
        }
      }
      return $options;
    }
96 97 98 99 100
  }

  /**
   * {@inheritdoc}
   */
101
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    return array(
      'columns' => array(
        'target_id' => array(
          'type' => 'int',
          'unsigned' => TRUE,
        ),
      ),
      'indexes' => array(
        'target_id' => array('target_id'),
      ),
      'foreign keys' => array(
        'target_id' => array(
          'table' => 'taxonomy_term_data',
          'columns' => array('target_id' => 'tid'),
        ),
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
124
  public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
125 126 127 128 129 130 131 132 133
    $vocabularies = entity_load_multiple('taxonomy_vocabulary');
    $options = array();
    foreach ($vocabularies as $vocabulary) {
      $options[$vocabulary->id()] = $vocabulary->name;
    }

    $element = array();
    $element['#tree'] = TRUE;

134
    foreach ($this->getSetting('allowed_values') as $delta => $tree) {
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
      $element['allowed_values'][$delta]['vocabulary'] = array(
        '#type' => 'select',
        '#title' => t('Vocabulary'),
        '#default_value' => $tree['vocabulary'],
        '#options' => $options,
        '#required' => TRUE,
        '#description' => t('The vocabulary which supplies the options for this field.'),
        '#disabled' => $has_data,
      );
      $element['allowed_values'][$delta]['parent'] = array(
        '#type' => 'value',
        '#value' => $tree['parent'],
      );
    }

    return $element;
  }

  /**
   * {@inheritdoc}
   */
156
  public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
157 158 159 160
    return array();
  }

}