/** * Generates the form element for a single copy of the widget. */ protected function formSingleElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $entity = $items->getEntity(); $element += array('#field_parents' => $form['#parents'], '#required' => $delta == 0 && $this->fieldDefinition->isRequired(), '#delta' => $delta, '#weight' => $delta); $element = $this->formElement($items, $delta, $element, $form, $form_state); if ($element) { // Allow modules to alter the field widget form element. $context = array('form' => $form, 'widget' => $this, 'items' => $items, 'delta' => $delta, 'default' => $this->isDefaultValueWidget($form_state)); \Drupal::moduleHandler()->alter(array('field_widget_form', 'field_widget_' . $this->getPluginId() . '_form'), $element, $form_state, $context); } return $element; }
/** * {@inheritdoc} */ public static function isApplicable(FieldDefinitionInterface $field_definition) { if (!$field_definition->isRequired()) { return FALSE; } $handler_settings = $field_definition->getSettings()['handler_settings']; // Entity types without bundles will throw notices on next condition so let's // stop before they do. We should support this kind of entities too. See // https://www.drupal.org/node/2569193 and remove this check once that issue // lands. if (empty($handler_settings['target_bundles'])) { return FALSE; } if (count($handler_settings['target_bundles']) != 1) { return FALSE; } return TRUE; }