You are here

class FieldCollectionEmbedWidget in Field collection 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldWidget/FieldCollectionEmbedWidget.php \Drupal\field_collection\Plugin\Field\FieldWidget\FieldCollectionEmbedWidget

Plugin implementation of the 'field_collection_embed' widget.

Plugin annotation


@FieldWidget(
  id = "field_collection_embed",
  label = @Translation("Embedded"),
  field_types = {
    "field_collection"
  },
)

Hierarchy

Expanded class hierarchy of FieldCollectionEmbedWidget

File

src/Plugin/Field/FieldWidget/FieldCollectionEmbedWidget.php, line 26

Namespace

Drupal\field_collection\Plugin\Field\FieldWidget
View source
class FieldCollectionEmbedWidget extends WidgetBase {

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

    // TODO: Detect recursion
    $field_name = $this->fieldDefinition
      ->getName();

    // Nest the field collection item entity form in a dedicated parent space,
    // by appending [field_name, delta] to the current parent space.
    // That way the form values of the field collection item are separated.
    $parents = array_merge($element['#field_parents'], [
      $field_name,
      $delta,
    ]);
    $element += [
      '#element_validate' => [
        [
          static::class,
          'validate',
        ],
      ],
      '#parents' => $parents,
      '#field_name' => $field_name,
    ];
    if ($this->fieldDefinition
      ->getFieldStorageDefinition()
      ->getCardinality() == 1) {
      $element['#type'] = 'fieldset';
    }
    $field_state = static::getWidgetState($element['#field_parents'], $field_name, $form_state);
    if (isset($field_state['field_collection_item'][$delta])) {
      $field_collection_item = $field_state['field_collection_item'][$delta];
    }
    else {
      $field_collection_item = $items[$delta]
        ->getFieldCollectionItem(TRUE);

      // Put our entity in the form state, so FAPI callbacks can access it.
      $field_state['field_collection_item'][$delta] = $field_collection_item;
    }
    static::setWidgetState($element['#field_parents'], $field_name, $form_state, $field_state);
    $display = entity_get_form_display('field_collection_item', $field_name, 'default');
    $display
      ->buildForm($field_collection_item, $element, $form_state);
    if (empty($element['#required'])) {
      $element['#after_build'][] = [
        static::class,
        'delayRequiredValidation',
      ];

      // Stop HTML5 form validation so our validation code can run instead.
      $form['#attributes']['novalidate'] = 'novalidate';
    }

    // Put the remove button on unlimited cardinality field collection fields.
    if ($this->fieldDefinition
      ->getFieldStorageDefinition()
      ->getCardinality() == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
      $options = [
        'query' => [
          'element_parents' => implode('/', $element['#parents']),
        ],
      ];
      $element['actions'] = [
        '#type' => 'actions',
        'remove_button' => [
          '#delta' => $delta,
          '#name' => implode('_', $parents) . '_remove_button',
          '#type' => 'submit',
          '#value' => t('Remove'),
          '#validate' => [],
          '#submit' => [
            [
              static::class,
              'removeSubmit',
            ],
          ],
          '#limit_validation_errors' => [],
          '#ajax' => [
            'callback' => [
              $this,
              'ajaxRemove',
            ],
            'options' => $options,
            'effect' => 'fade',
            'wrapper' => $form['#wrapper_id'],
          ],
          '#weight' => 1000,
        ],
      ];
    }
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {

    // We don't want to render empty items on field collection fields
    // unless a) the field collection is empty ; b) the form is rebuilding,
    // which means that the user clicked on "Add another item"; or
    // c) we are creating a new entity.
    if (count($items) > 0 && !$form_state
      ->isRebuilding() && !$items
      ->getEntity()
      ->isNew()) {
      $field_name = $this->fieldDefinition
        ->getName();
      $cardinality = $this->fieldDefinition
        ->getFieldStorageDefinition()
        ->getCardinality();
      $parents = $form['#parents'];
      if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
        $field_state = static::getWidgetState($parents, $field_name, $form_state);
        $field_state['items_count']--;
        static::setWidgetState($parents, $field_name, $form_state, $field_state);
      }
    }

    // Adjust wrapper identifiers as they are shared between parents and
    // children in nested field collections.
    $form['#wrapper_id'] = Html::getUniqueID($items
      ->getName());
    $elements = parent::formMultipleElements($items, $form, $form_state);
    $elements['#prefix'] = '<div id="' . $form['#wrapper_id'] . '">';
    $elements['#suffix'] = '</div>';
    $elements['add_more']['#ajax']['wrapper'] = $form['#wrapper_id'];
    return $elements;
  }

  /**
   * #after_build of a field collection element.
   *
   * Delays the validation of #required.
   */
  public static function delayRequiredValidation($element, FormStateInterface $form_state) {

    // If the process_input flag is set, the form and its input is going to be
    // validated. Prevent #required (sub)fields from throwing errors while
    // their non-#required field collection item is empty.
    if ($form_state
      ->isProcessingInput()) {
      static::collectRequiredElements($element, $element['#field_collection_required_elements']);
    }
    return $element;
  }

  /**
   * Prevent the default 'required' validation from running on subfields.
   */
  private static function collectRequiredElements(&$element, &$required_elements) {

    // Recurse through all children.
    foreach (Element::children($element) as $key) {
      if (isset($element[$key]) && $element[$key]) {
        static::collectRequiredElements($element[$key], $required_elements);
      }
    }
    if (!empty($element['#required'])) {
      $element['#required'] = FALSE;
      $required_elements[] =& $element;
      $element += [
        '#pre_render' => [],
      ];
      array_unshift($element['#pre_render'], [
        static::class,
        'renderRequired',
      ]);
    }
  }

  /**
   * #pre_render callback ensures the element is rendered as being required.
   */
  public static function renderRequired($element) {
    $element['#required'] = TRUE;
    return $element;
  }

  /**
   * FAPI validation of an individual field collection element.
   */
  public static function validate($element, FormStateInterface $form_state, $form) {
    $field_parents = $element['#field_parents'];
    $field_name = $element['#field_name'];
    $field_state = static::getWidgetState($field_parents, $field_name, $form_state);
    $field_collection_item = $field_state['field_collection_item'][$element['#delta']];
    $display = entity_get_form_display('field_collection_item', $field_name, 'default');
    $display
      ->extractFormValues($field_collection_item, $element, $form_state);

    // Now validate required elements if the entity is not empty.
    if (!$field_collection_item
      ->isEmpty() && !empty($element['#field_collection_required_elements'])) {
      foreach ($element['#field_collection_required_elements'] as &$elements) {

        // Copied from \Drupal\Core\Form\FormValidator::doValidateForm().
        // #1676206: Modified to support options widget.
        if (isset($elements['#needs_validation'])) {
          $is_countable = is_array($elements['#value']) || $elements['#value'] instanceof \Countable;
          $is_empty_multiple = $is_countable && count($elements['#value']) == 0;

          // @todo: replace Unicode::strlen() with mb_substr() per
          // https://www.drupal.org/project/drupal/issues/2849669
          $is_empty_string = is_string($elements['#value']) && Unicode::strlen(trim($elements['#value'])) == 0;
          $is_empty_value = $elements['#value'] === 0;
          $is_empty_option = isset($elements['#options']['_none']) && $elements['#value'] == '_none';
          if ($is_empty_multiple || $is_empty_string || $is_empty_value || $is_empty_option) {
            if (isset($elements['#required_error'])) {
              $form_state
                ->setError($elements, $elements['#required_error']);
            }
            else {
              if (isset($elements['#title'])) {
                $form_state
                  ->setError($elements, t('@name field is required.', [
                  '@name' => $elements['#title'],
                ]));
              }
              else {
                $form_state
                  ->setError($elements);
              }
            }
          }
        }
      }
    }

    // Only if the form is being submitted, finish the collection entity and
    // prepare it for saving.
    if ($form_state
      ->isSubmitted() && !$form_state
      ->hasAnyErrors()) {

      // Load initial form values into $item, so any other form values below the
      // same parents are kept.
      $field = NestedArray::getValue($form_state
        ->getValues(), $element['#parents']);

      // Set the _weight if it is a multiple field.
      $element_widget = NestedArray::getValue($form, array_slice($element['#array_parents'], 0, -1));
      if (isset($element['_weight']) && $element_widget['#cardinality_multiple']) {
        $field['_weight'] = $element['_weight']['#value'];
      }

      // Put the field collection field in $field['field_collection_item'], so
      // it is saved with the host entity via FieldCollection->preSave() / field
      // API if it is not empty.
      $field['field_collection_item'] = $field_collection_item;
      $form_state
        ->setValue($element['#parents'], $field);
    }
  }

  /**
   * Submit callback to remove an item from the field UI multiple wrapper.
   *
   * When a remove button is submitted, we need to find the item that it
   * referenced and delete it. Since field UI has the deltas as a straight
   * unbroken array key, we have to renumber everything down. Since we do this
   * we *also* need to move all the deltas around in the $form_state->values
   * and $form_state input so that user changed values follow. This is a bit
   * of a complicated process.
   */
  public static function removeSubmit($form, FormStateInterface $form_state) {
    $button = $form_state
      ->getTriggeringElement();
    $delta = $button['#delta'];

    // Where in the form we'll find the parent element.
    $address = array_slice($button['#array_parents'], 0, -4);
    $address_state = array_slice($button['#parents'], 0, -3);

    // Go one level up in the form, to the widgets container.
    $parent_element = NestedArray::getValue($form, array_merge($address, [
      'widget',
    ]));
    $field_name = $parent_element['#field_name'];
    $parents = $parent_element['#field_parents'];
    $field_state = static::getWidgetState($parents, $field_name, $form_state);

    // Go ahead and renumber everything from our delta to the last
    // item down one. This will overwrite the item being removed.
    for ($i = $delta; $i <= $field_state['items_count']; $i++) {
      $old_element_address = array_merge($address, [
        'widget',
        $i + 1,
      ]);
      $old_element_state_address = array_merge($address_state, [
        $i + 1,
      ]);
      $new_element_state_address = array_merge($address_state, [
        $i,
      ]);
      $moving_element = NestedArray::getValue($form, $old_element_address);
      $moving_element_value = NestedArray::getValue($form_state
        ->getValues(), $old_element_state_address);
      $moving_element_input = NestedArray::getValue($form_state
        ->getUserInput(), $old_element_state_address);

      // Tell the element where it's being moved to.
      $moving_element['#parents'] = $new_element_state_address;

      // Move the element around.
      $form_state
        ->setValueForElement($moving_element, $moving_element_value);
      $user_input = $form_state
        ->getUserInput();
      NestedArray::setValue($user_input, $moving_element['#parents'], $moving_element_input);
      $form_state
        ->setUserInput($user_input);

      // Move the entity in our saved state.
      if (isset($field_state['field_collection_item'][$i + 1])) {
        $field_state['field_collection_item'][$i] = $field_state['field_collection_item'][$i + 1];
      }
      else {
        unset($field_state['field_collection_item'][$i]);
      }
    }

    // Then remove the last item. But we must not go negative.
    if ($field_state['items_count'] > 0) {
      $field_state['items_count']--;
    }
    else {

      // Create a new field collection item after deleting the last one so the
      // form will show a blank field collection item instead of resurrecting
      // the first one if there was already data.
      $field_state['field_collection_item'][0] = FieldCollectionItem::create([
        'field_name' => $field_name,
      ]);
    }

    // Fix the weights. Field UI lets the weights be in a range of
    // (-1 * item_count) to (item_count). This means that when we remove one,
    // the range shrinks; weights outside of that range then get set to
    // the first item in the select by the browser, floating them to the top.
    // We use a brute force method because we lost weights on both ends
    // and if the user has moved things around, we have to cascade because
    // if I have items weight weights 3 and 4, and I change 4 to 3 but leave
    // the 3, the order of the two 3s now is undefined and may not match what
    // the user had selected.
    $input = NestedArray::getValue($form_state
      ->getUserInput(), $address_state);

    // Sort by weight.
    uasort($input, '_field_collection_sort_items_helper');

    // Reweight everything in the correct order.
    $weight = -1 * $field_state['items_count'];
    foreach ($input as $key => $item) {
      if ($item) {
        $input[$key]['_weight'] = $weight++;
      }
    }
    $user_input = $form_state
      ->getUserInput();
    NestedArray::setValue($user_input, $address_state, $input);
    $form_state
      ->setUserInput($user_input);
    static::setWidgetState($parents, $field_name, $form_state, $field_state);
    $form_state
      ->setRebuild();
  }

  /**
   * Ajax callback to remove a field collection from a multi-valued field.
   *
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   An AjaxResponse object.
   *
   * @see self::removeSubmit()
   */
  function ajaxRemove(array $form, FormStateInterface &$form_state) {

    // At this point, $this->removeSubmit() removed the element so we just need
    // to return the parent element.
    $button = $form_state
      ->getTriggeringElement();
    return NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -3));
  }

  /**
   * Submission handler for the "Add another item" button.
   */
  public static function addMoreSubmit(array $form, FormStateInterface $form_state) {
    $button = $form_state
      ->getTriggeringElement();

    // Go one level up in the form, to the widgets container.
    $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));
    $field_name = $element['#field_name'];
    $parents = $element['#field_parents'];

    // Increment the items count.
    $field_state = static::getWidgetState($parents, $field_name, $form_state);
    $field_state['items_count']++;

    // Make a new field collection item. This helps to ensure that
    // trying to add a field collection item won't ressurect a deleted one from
    // the trash bin.
    $field_state['field_collection_item'][$field_state['items_count']] = FieldCollectionItem::create([
      'field_name' => $field_name,
    ]);
    static::setWidgetState($parents, $field_name, $form_state, $field_state);
    $form_state
      ->setRebuild();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AllowedTagsXssTrait::allowedTags public function Returns a list of tags allowed by AllowedTagsXssTrait::fieldFilterXss().
AllowedTagsXssTrait::displayAllowedTags public function Returns a human-readable list of allowed tags for display in help texts.
AllowedTagsXssTrait::fieldFilterXss public function Filters an HTML string to prevent XSS vulnerabilities.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FieldCollectionEmbedWidget::addMoreSubmit public static function Submission handler for the "Add another item" button. Overrides WidgetBase::addMoreSubmit
FieldCollectionEmbedWidget::ajaxRemove function Ajax callback to remove a field collection from a multi-valued field.
FieldCollectionEmbedWidget::collectRequiredElements private static function Prevent the default 'required' validation from running on subfields.
FieldCollectionEmbedWidget::delayRequiredValidation public static function #after_build of a field collection element.
FieldCollectionEmbedWidget::formElement public function Returns the form for a single field widget. Overrides WidgetInterface::formElement
FieldCollectionEmbedWidget::formMultipleElements protected function Special handling to create form elements for multiple values. Overrides WidgetBase::formMultipleElements
FieldCollectionEmbedWidget::removeSubmit public static function Submit callback to remove an item from the field UI multiple wrapper.
FieldCollectionEmbedWidget::renderRequired public static function #pre_render callback ensures the element is rendered as being required.
FieldCollectionEmbedWidget::validate public static function FAPI validation of an individual field collection element.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::$thirdPartySettings protected property The plugin settings injected by third party modules.
PluginSettingsBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 6
PluginSettingsBase::defaultSettings public static function Defines the default settings for this plugin. Overrides PluginSettingsInterface::defaultSettings 42
PluginSettingsBase::getSetting public function Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::getThirdPartyProviders public function Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface::getThirdPartyProviders
PluginSettingsBase::getThirdPartySetting public function Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface::getThirdPartySetting
PluginSettingsBase::getThirdPartySettings public function Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface::getThirdPartySettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::onDependencyRemoval public function Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface::onDependencyRemoval 3
PluginSettingsBase::setSetting public function Sets the value of a setting for the plugin. Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Sets the settings for the plugin. Overrides PluginSettingsInterface::setSettings
PluginSettingsBase::setThirdPartySetting public function Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface::setThirdPartySetting
PluginSettingsBase::unsetThirdPartySetting public function Unsets a third-party setting. Overrides ThirdPartySettingsInterface::unsetThirdPartySetting
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WidgetBase::$fieldDefinition protected property The field definition.
WidgetBase::$settings protected property The widget settings. Overrides PluginSettingsBase::$settings
WidgetBase::addMoreAjax public static function Ajax callback for the "Add another item" button.
WidgetBase::afterBuild public static function After-build handler for field elements in a form.
WidgetBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 5
WidgetBase::errorElement public function Assigns a field-level validation error to the right widget sub-element. Overrides WidgetInterface::errorElement 8
WidgetBase::extractFormValues public function Extracts field values from submitted form values. Overrides WidgetBaseInterface::extractFormValues 2
WidgetBase::flagErrors public function Reports field-level validation errors against actual form elements. Overrides WidgetBaseInterface::flagErrors 2
WidgetBase::form public function Creates a form element for a field. Overrides WidgetBaseInterface::form 3
WidgetBase::formSingleElement protected function Generates the form element for a single copy of the widget.
WidgetBase::getFieldSetting protected function Returns the value of a field setting.
WidgetBase::getFieldSettings protected function Returns the array of field settings.
WidgetBase::getFilteredDescription protected function Returns the filtered field description.
WidgetBase::getWidgetState public static function Retrieves processing information about the widget from $form_state. Overrides WidgetBaseInterface::getWidgetState
WidgetBase::getWidgetStateParents protected static function Returns the location of processing information within $form_state.
WidgetBase::handlesMultipleValues protected function Returns whether the widget handles multiple values.
WidgetBase::isApplicable public static function Returns if the widget can be used for the provided field. Overrides WidgetInterface::isApplicable 4
WidgetBase::isDefaultValueWidget protected function Returns whether the widget used for default value form.
WidgetBase::massageFormValues public function Massages the form values into the format expected for field values. Overrides WidgetInterface::massageFormValues 7
WidgetBase::settingsForm public function Returns a form to configure settings for the widget. Overrides WidgetInterface::settingsForm 16
WidgetBase::settingsSummary public function Returns a short summary for the current widget settings. Overrides WidgetInterface::settingsSummary 15
WidgetBase::setWidgetState public static function Stores processing information about the widget in $form_state. Overrides WidgetBaseInterface::setWidgetState
WidgetBase::__construct public function Constructs a WidgetBase object. Overrides PluginBase::__construct 5