'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(); } }