You are here

public function MenuLinkContentVisibilityWidget::massageFormValues in Custom Menu Links Visibility 8

Same name and namespace in other branches
  1. 2.x src/Plugin/Field/FieldWidget/MenuLinkContentVisibilityWidget.php \Drupal\menu_link_content_visibility\Plugin\Field\FieldWidget\MenuLinkContentVisibilityWidget::massageFormValues()

@inheritDoc

Overrides WidgetBase::massageFormValues

File

src/Plugin/Field/FieldWidget/MenuLinkContentVisibilityWidget.php, line 95

Class

MenuLinkContentVisibilityWidget
Plugin annotation @FieldWidget( label = @Translation("Menu link visibility"), id = "menu_link_content_visibility", field_types = { "menu_link_content_visibility" }, )

Namespace

Drupal\menu_link_content_visibility\Plugin\Field\FieldWidget

Code

public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
  foreach ($values as $delta => $value) {
    unset($value['_original_delta']);
    foreach ($value as $condition_id => $condition_configuration) {

      /** @var ConditionInterface $condition */
      $condition = $this->condition_manager
        ->createInstance($condition_id);
      $condition
        ->setConfiguration($condition_configuration);
      $field_name = $this->fieldDefinition
        ->getName();
      $subform = $form[$field_name]['widget'][$delta][$condition_id];
      $subform_state = SubformState::createForSubform($subform, $form, $form_state);
      $condition
        ->submitConfigurationForm($subform, $subform_state);
      $comparable_configuration = $condition
        ->getConfiguration();
      unset($comparable_configuration['id']);
      unset($comparable_configuration['context_mapping']);
      if ($comparable_configuration != $condition
        ->defaultConfiguration()) {
        $value[$condition_id] = $condition
          ->getConfiguration();
      }
      else {
        unset($value[$condition_id]);
      }
    }
    if (!empty($value)) {
      $values[$delta] = serialize($value);
    }
    else {
      unset($values[$delta]);
    }
  }
  return $values;
}