You are here

public function BlockFieldWidget::massageFormValues in Block field 8

Massages the form values into the format expected for field values.

Parameters

array $values: The submitted form values produced by the widget.

  • If the widget does not manage multiple values itself, the array holds the values generated by the multiple copies of the $element generated by the formElement() method, keyed by delta.
  • If the widget manages multiple values, the array holds the values of the form element generated by the formElement() method.

array $form: The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Return value

array An array of field values, keyed by delta.

Overrides WidgetBase::massageFormValues

File

src/Plugin/Field/FieldWidget/BlockFieldWidget.php, line 272

Class

BlockFieldWidget
Plugin implementation of the 'block_field' widget.

Namespace

Drupal\block_field\Plugin\Field\FieldWidget

Code

public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
  $field_name = $this->fieldDefinition
    ->getName();

  // Some blocks clean the processed values in form state. However, entity
  // forms extract the form values twice during submission. For the second
  // submission to work as well, we need to prevent the removal of the form
  // values during the first submission.
  $form_state = clone $form_state;
  foreach ($values as &$value) {

    // Execute block submit configuration in order to transform the form
    // values into block configuration.
    if (!empty($value['plugin_id']) && !empty($value['settings']) && ($block = $this->blockManager
      ->createInstance($value['plugin_id']))) {
      $elements =& $form[$field_name]['widget'][$value['_original_delta']]['settings'];
      $subform_state = SubformState::createForSubform($elements, $form_state
        ->getCompleteForm(), $form_state);
      $block
        ->submitConfigurationForm($elements, $subform_state);

      // If this block is context-aware, set the context mapping.
      if ($block instanceof ContextAwarePluginInterface && $block
        ->getContextDefinitions()) {
        $context_mapping = $subform_state
          ->getValue('context_mapping', []);
        $block
          ->setContextMapping($context_mapping);
      }
      $value['settings'] = $block
        ->getConfiguration();
    }
  }
  return $values;
}