summaryrefslogtreecommitdiffstats
path: root/core/modules/taxonomy/src/Plugin/Field/FieldType/TaxonomyTermReferenceItem.php
blob: 75051c9827f3c26b4446812e8221c7c0235e9c01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
109
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php

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

namespace Drupal\taxonomy\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\OptGroup;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\OptionsProviderInterface;

/**
 * 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",
 *   list_class = "\Drupal\Core\Field\EntityReferenceFieldItemList",
 *   constraints = {"ValidReference" = {}}
 * )
 */
class TaxonomyTermReferenceItem extends EntityReferenceItem implements OptionsProviderInterface {

  /**
   * {@inheritdoc}
   */
  public static function defaultStorageSettings() {
    return array(
      'target_type' => 'taxonomy_term',
      'options_list_callback' => NULL,
      'allowed_values' => array(
        array(
          'vocabulary' => '',
          'parent' => 0,
        ),
      ),
    ) + parent::defaultStorageSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function getPossibleValues(AccountInterface $account = NULL) {
    // Flatten options firstly, because Possible Options may contain group
    // arrays.
    $flatten_options = OptGroup::flattenOptions($this->getPossibleOptions($account));
    return array_keys($flatten_options);
  }

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

  /**
   * {@inheritdoc}
   */
  public function getSettableValues(AccountInterface $account = NULL) {
    // Flatten options firstly, because Settable Options may contain group
    // arrays.
    $values = array_keys(OptGroup::flattenOptions($this->getSettableOptions($account)));
    $values[] = static::$NEW_ENTITY_MARKER;
    return $values;
  }

  /**
   * {@inheritdoc}
   */
  public function getSettableOptions(AccountInterface $account = NULL) {
    if ($callback = $this->getSetting('options_list_callback')) {
      return call_user_func_array($callback, array($this->getFieldDefinition(), $this->getEntity()));
    }
    else {
      $options = array();
      foreach ($this->getSetting('allowed_values') as $tree) {
        if ($vocabulary = entity_load('taxonomy_vocabulary', $tree['vocabulary'])) {
          if ($terms = taxonomy_get_tree($vocabulary->id(), $tree['parent'], NULL, TRUE)) {
            foreach ($terms as $term) {
              $options[$term->id()] = str_repeat('-', $term->depth) . $term->getName();
            }
          }
        }
      }
      return $options;
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    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}
   */
  public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
    $vocabularies = entity_load_multiple('taxonomy_vocabulary');
    $options = array();
    foreach ($vocabularies as $vocabulary) {
      $options[$vocabulary->id()] = $vocabulary->name;
    }

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

    foreach ($this->getSetting('allowed_values') as $delta => $tree) {
      $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}
   */
  public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
    return array();
  }

}