You are here

public function FlaggingForm::actions in Flag 8.4

Returns an array of supported actions for the current entity form.

This function generates a list of Form API elements which represent actions supported by the current entity form.

@todo Consider introducing a 'preview' action here, since it is used by many entity types.

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

array An array of supported Form API action elements keyed by name.

Overrides EntityForm::actions

File

src/Form/FlaggingForm.php, line 27

Class

FlaggingForm
Provides the flagging form for field entry.

Namespace

Drupal\flag\Form

Code

public function actions(array $form, FormStateInterface $form_state) {
  $actions = parent::actions($form, $form_state);

  /** @var \Drupal\flag\FlaggingInterface $flagging */
  $flagging = $this
    ->getEntity();
  $flag = $flagging
    ->getFlag();
  $action_link = $flag
    ->getLinkTypePlugin();
  $create_button_text = $this
    ->t('Create Flagging');
  if ($action_link instanceof FormEntryInterface) {
    $create_button_text = $action_link
      ->getCreateButtonText();
  }
  if ($this->entity
    ->isNew()) {
    $actions['submit']['#value'] = $create_button_text;
  }
  else {
    $update_button_text = $this
      ->t('Update Flagging');
    if ($action_link instanceof FormEntryInterface) {
      $update_button_text = $action_link
        ->getUpdateButtonText();
    }
    $actions['submit']['#value'] = $update_button_text;
  }

  // Customize the delete link.
  if (isset($actions['delete'])) {

    // @todo Why does the access call always fail?
    unset($actions['delete']['#access']);
    $delete_button_text = $this
      ->t('Delete Flagging');
    if ($action_link instanceof FormEntryInterface) {
      $delete_button_text = $action_link
        ->getDeleteButtonText();
    }
    $actions['delete']['#title'] = $delete_button_text;

    // Build the delete url from route. We need to build this manually
    // otherwise Drupal will try to build the flagging entity's delete-form
    // link. Since that route doesn't use the flagging ID, Drupal can't build
    // the link for us.
    $route_params = [
      'flag' => $this->entity
        ->getFlagId(),
      'entity_id' => $this->entity
        ->getFlaggableId(),
      'destination' => $this
        ->getRequest()->request
        ->get('destination'),
    ];
    $url = Url::fromRoute('flag.field_entry.delete', $route_params);
    $actions['delete']['#url'] = $url;
  }
  return $actions;
}