You are here

public function MenuPositionRuleForm::save in Menu Position 8

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/Form/MenuPositionRuleForm.php, line 288

Class

MenuPositionRuleForm
The Menu Position rule form.

Namespace

Drupal\menu_position\Form

Code

public function save(array $form, FormStateInterface $form_state) {

  // Get menu position rule.

  /* @var \Drupal\menu_position\Entity\MenuPositionRule $rule */
  $rule = $this->entity;
  $is_new = $rule
    ->isNew();
  $menu_link_id = 'menu_position_link:' . $rule
    ->id();
  if (!$this->menu_link_manager
    ->hasDefinition($menu_link_id)) {
    $rule
      ->setMenuLink($menu_link_id);
    $rule
      ->save();

    // Let the deriver generate the menu link.
    $this->menu_link_manager
      ->rebuild();
  }
  $definition = $this->menu_link_manager
    ->getDefinition($menu_link_id);
  $menu_link = $this->menu_link_manager
    ->updateDefinition($menu_link_id, [
    'menu_name' => $form_state
      ->getValue('menu_name'),
    'parent' => $form_state
      ->getValue('parent'),
  ] + $definition);

  // Submit visibility condition settings.
  foreach ($form_state
    ->getValue('conditions') as $condition_id => $values) {

    // Allow the condition to submit the form.
    $condition = $form_state
      ->get([
      'conditions',
      $condition_id,
    ]);
    $condition_values = SubformState::createForSubform($form['conditions'][$condition_id], $form, $form_state);
    $condition
      ->submitConfigurationForm($form['conditions'][$condition_id], $condition_values);

    // Set context mapping values.
    if ($condition instanceof ContextAwarePluginInterface) {
      $context_mapping = isset($values['context_mapping']) ? $values['context_mapping'] : [];
      $condition
        ->setContextMapping($context_mapping);
    }

    // Update the original form values.
    $condition_configuration = $condition
      ->getConfiguration();

    // Update the conditions on the menu position rule.
    $rule
      ->getConditions()
      ->addInstanceId($condition_id, $condition_configuration);
  }

  // Save the menu position rule and get the status for messaging.
  $status = $rule
    ->save();
  if ($status && $is_new) {
    $this->messenger
      ->addMessage($this
      ->t('Rule %label has been added.', [
      '%label' => $rule
        ->getLabel(),
    ]));
  }
  elseif ($status) {
    $this->messenger
      ->addMessage($this
      ->t('Rule %label has been updated.', [
      '%label' => $rule
        ->getLabel(),
    ]));
  }
  else {
    $this->messenger
      ->addWarning($this
      ->t('Rule %label was not saved.', [
      '%label' => $rule
        ->getLabel(),
    ]));
  }

  // Flush appropriate menu cache.
  $this->route_builder
    ->rebuild();

  // Redirect back to the menu position rule order form.
  $form_state
    ->setRedirect('entity.menu_position_rule.order_form');
}