You are here

public function ItemForm::save in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Form/ItemForm.php \Drupal\business_rules\Form\ItemForm::save()

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

1 call to ItemForm::save()
ConditionForm::save in src/Form/ConditionForm.php
Additional steps to save condition's items.
1 method overrides ItemForm::save()
ConditionForm::save in src/Form/ConditionForm.php
Additional steps to save condition's items.

File

src/Form/ItemForm.php, line 407

Class

ItemForm
Base class for Business rules item.

Namespace

Drupal\business_rules\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  if ($this->step < 2 && $this->entity
    ->isNew()) {
    $this->step++;
    $form_state
      ->setRebuild();
    return $form;
  }
  else {

    /** @var \Drupal\business_rules\ItemInterface $item */
    $item = $this->entity;
    $itemManager = $this
      ->getItemManager();

    // Put the settings fields in an array format to save the ConfigEntity.
    // @TODO change it to use the item's schema.
    $settings = [];
    foreach ($form['settings'] as $key => $value) {
      if (substr($key, 0, 1) != '#' && array_key_exists($key, $form_state
        ->getValues()) && !in_array($key, [
        'target_entity_type',
        'target_bundle',
      ])) {
        $settings[$key] = $form_state
          ->getValue($key);
      }
    }
    $type = $item
      ->getType() ? $item
      ->getType() : $form_state
      ->getValue('type');
    $definition = $itemManager
      ->getDefinition($type);
    $reflection = new \ReflectionClass($definition['class']);
    $custom_item = $reflection
      ->newInstance($definition, $definition['id'], $definition);
    $settings = $custom_item
      ->processSettings($settings, $item);
    $item
      ->set('settings', $settings);
    $item
      ->setTags(explode(',', $form_state
      ->getValue('tags')));
    $status = $item
      ->save();

    // As the item may need to be executed under a cached hook, we need to
    // invalidate all rendered caches.
    Cache::invalidateTags([
      'rendered',
    ]);
    switch ($status) {
      case SAVED_NEW:
        $this
          ->messenger()
          ->addMessage($this
          ->t('Created the %label Item.', [
          '%label' => $item
            ->label(),
        ]));
        break;
      default:
        $this
          ->messenger()
          ->addMessage($this
          ->t('Saved the %label Item.', [
          '%label' => $item
            ->label(),
        ]));
    }
    if (isset($form_state
      ->getTriggeringElement()['#op'])) {
      $op = $form_state
        ->getTriggeringElement()['#op'];
      if ($op == 'save') {
        $form_state
          ->setRedirectUrl($custom_item
          ->getEditUrl($item));
      }
      else {
        $form_state
          ->setRedirectUrl($custom_item
          ->getRedirectUrl($item));
      }
    }
  }
  return $status;
}