Created
October 12, 2016 09:12
-
-
Save dawehner/7d4f565970fbca4116e23be231c4d411 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Drupal\foo_video\Plugin\Field\FieldWidget; | |
| use Drupal\Core\Entity\FieldableEntityInterface; | |
| use Drupal\Core\Field\FieldDefinitionInterface; | |
| use Drupal\Core\Field\FieldItemListInterface; | |
| use Drupal\Core\Field\WidgetBase; | |
| use Drupal\Core\Form\FormStateInterface; | |
| use Drupal\Core\Form\OptGroup; | |
| /** | |
| * Provides a options widget that allows that selects the first available option. | |
| * | |
| * @FieldWidget( | |
| * id = "hidden_option_first_value", | |
| * label = @Translation("Hidden option first value"), | |
| * field_types = { | |
| * "entity_reference", | |
| * }, | |
| * ) | |
| */ | |
| class HiddenOptionFirstValue extends WidgetBase { | |
| /** | |
| * abstract over the actual field columns, to allow different field types to | |
| * reuse those widgets. | |
| * | |
| * @var string | |
| */ | |
| protected $column; | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings) { | |
| parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings); | |
| $property_names = $this->fieldDefinition->getFieldStorageDefinition()->getPropertyNames(); | |
| $this->column = $property_names[0]; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { | |
| $entity = $items->getEntity(); | |
| $options = $this->getOptions($entity); | |
| $element['target_id'] = $element + [ | |
| '#type' => 'value', | |
| '#value' => (string) reset($options), | |
| ]; | |
| return $element; | |
| } | |
| public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { | |
| return $values; | |
| } | |
| public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) { | |
| return parent::extractFormValues($items, $form, $form_state); | |
| } | |
| /** | |
| * Returns the array of options for the widget. | |
| * | |
| * @param \Drupal\Core\Entity\FieldableEntityInterface $entity | |
| * The entity for which to return options. | |
| * | |
| * @return array | |
| * The array of options for the widget. | |
| */ | |
| protected function getOptions(FieldableEntityInterface $entity) { | |
| if (!isset($this->options)) { | |
| // Limit the settable options for the current user account. | |
| $options = $this->fieldDefinition | |
| ->getFieldStorageDefinition() | |
| ->getOptionsProvider($this->column, $entity) | |
| ->getSettableOptions(\Drupal::currentUser()); | |
| $module_handler = \Drupal::moduleHandler(); | |
| $context = [ | |
| 'fieldDefinition' => $this->fieldDefinition, | |
| 'entity' => $entity, | |
| ]; | |
| $module_handler->alter('options_list', $options, $context); | |
| // Options might be nested ("optgroups"). If the widget does not support | |
| // nested options, flatten the list. | |
| $options = OptGroup::flattenOptions($options); | |
| $this->options = $options; | |
| } | |
| return $this->options; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment