You are here

public function EckEntityForm::save in Entity Construction Kit (ECK) 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/Entity/EckEntityForm.php, line 37

Class

EckEntityForm
Form controller for the ECK entity forms.

Namespace

Drupal\eck\Form\Entity

Code

public function save(array $form, FormStateInterface $form_state) {
  $saved = parent::save($form, $form_state);
  $logger = $this
    ->logger('eck');
  $entity_link = $this->entity
    ->toLink($this
    ->t('View'))
    ->toString();
  $context = [
    '@type' => $this->entity
      ->bundle(),
    'link' => $entity_link,
  ];
  if ($label = $this->entity
    ->label()) {
    $context['%label'] = $label;
    $t_args = [
      '@type' => $this->entity->type->entity
        ->label(),
      '%label' => $this->entity
        ->toLink($label)
        ->toString(),
    ];
    if ($saved === SAVED_NEW) {
      $logger
        ->notice('@type: added %label.', $context);
      $this
        ->messenger()
        ->addStatus($this
        ->t('@type %label has been created.', $t_args));
    }
    else {
      $logger
        ->notice('@type: updated %label', $context);
      $this
        ->messenger()
        ->addStatus($this
        ->t('@type %label has been updated.', $t_args));
    }
  }
  else {
    $t_args = [
      '%type' => $this->entity
        ->toLink($this->entity->type->entity
        ->label())
        ->toString(),
    ];
    if ($saved === SAVED_NEW) {
      $logger
        ->notice('@type: added entity.', $context);
      $this
        ->messenger()
        ->addStatus($this
        ->t('%type has been created.', $t_args));
    }
    else {
      $logger
        ->notice('@type: updated entity.', $context);
      $this
        ->messenger()
        ->addStatus($this
        ->t('%type has been updated.', $t_args));
    }
  }
  $form_state
    ->setRedirect('entity.' . $this->entity
    ->getEntityTypeId() . '.canonical', [
    $this->entity
      ->getEntityTypeId() => $this->entity
      ->id(),
  ]);
}