diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/field_type/TaxonomyTermReferenceItem.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/field_type/TaxonomyTermReferenceItem.php index 1ea9e2e..3997007 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/field_type/TaxonomyTermReferenceItem.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/field_type/TaxonomyTermReferenceItem.php @@ -36,13 +36,16 @@ * default_formatter = "taxonomy_term_reference_link" * ) */ -class TaxonomyTermReferenceItem extends EntityReferenceItem implements ConfigFieldItemInterface, AllowedValuesInterface{ +class TaxonomyTermReferenceItem extends EntityReferenceItem implements ConfigFieldItemInterface, AllowedValuesInterface { /** * {@inheritdoc} */ public function getPossibleValues(AccountInterface $account = NULL) { - return array_keys($this->getPossibleOptions($account)); + // Flatten options firstly, because Possible Options may contain group + // arrays. + $flatten_options = $this->flattenOptions($this->getPossibleOptions($account)); + return array_keys($flatten_options); } /** @@ -56,7 +59,10 @@ public function getPossibleOptions(AccountInterface $account = NULL) { * {@inheritdoc} */ public function getSettableValues(AccountInterface $account = NULL) { - return array_keys($this->getSettableOptions($account)); + // Flatten options firstly, because Settable Options may contain group + // arrays. + $flatten_options = $this->flattenOptions($this->getSettableOptions($account)); + return array_keys($flatten_options); } /** @@ -113,11 +119,11 @@ public function settingsForm(array $form, array &$form_state, $has_data) { } $settings = $this->getFieldSettings(); - $form = array(); - $form['#tree'] = TRUE; + $element = array(); + $element['#tree'] = TRUE; foreach ($settings['allowed_values'] as $delta => $tree) { - $form['allowed_values'][$delta]['vocabulary'] = array( + $element['allowed_values'][$delta]['vocabulary'] = array( '#type' => 'select', '#title' => t('Vocabulary'), '#default_value' => $tree['vocabulary'], @@ -126,13 +132,13 @@ public function settingsForm(array $form, array &$form_state, $has_data) { '#description' => t('The vocabulary which supplies the options for this field.'), '#disabled' => $has_data, ); - $form['allowed_values'][$delta]['parent'] = array( + $element['allowed_values'][$delta]['parent'] = array( '#type' => 'value', '#value' => $tree['parent'], ); } - return $form; + return $element; } /** @@ -165,6 +171,11 @@ public function preSave() { /** * {@inheritdoc} + * + * Taxonomy field settings allow for either a single vocabulary ID, multiple + * vocabulary IDs, or sub-trees of a vocabulary to be specified as allowed + * values, although only the first of these is supported via the field UI. + * Confirm that terms entered as values meet at least one of these conditions. */ public function getConstraints() { $constraint_manager = \Drupal::typedData()->getValidationConstraintManager(); @@ -184,4 +195,19 @@ public function getConstraints() { return $constraints; } + /** + * Flattens an array of allowed values. + * + * @param array $array + * A single or multidimensional array. + * + * @return array + * The flattened array. + */ + protected function flattenOptions(array $array) { + $result = array(); + array_walk_recursive($array, function($a, $b) use (&$result) { $result[$b] = $a; }); + return $result; + } + } diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index 648befd..4ca5676 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -917,6 +917,24 @@ function taxonomy_autocomplete_validate($element, &$form_state) { } /** + * @defgroup taxonomy_index Taxonomy indexing + * @{ + * Functions to maintain taxonomy indexing. + * + * Taxonomy uses default field storage to store canonical relationships + * between terms and fieldable entities. However its most common use case + * requires listing all content associated with a term or group of terms + * sorted by creation date. To avoid slow queries due to joining across + * multiple node and field tables with various conditions and order by criteria, + * we maintain a denormalized table with all relationships between terms, + * published nodes and common sort criteria such as sticky and created. + * This is used as a lookup table by taxonomy_select_nodes(). When using other + * field storage engines or alternative methods of denormalizing this data + * you should set the taxonomy.settings:maintain_index_table to '0' to avoid + * unnecessary writes in SQL. + */ + +/** * Implements hook_node_insert(). */ function taxonomy_node_insert(EntityInterface $node) {