association-1.0.0-alpha2/modules/association_menu/src/Form/MenuItemDeleteConfirm.php

modules/association_menu/src/Form/MenuItemDeleteConfirm.php
<?php

namespace Drupal\association_menu\Form;

use Drupal\association\Entity\AssociationInterface;
use Drupal\association_menu\AssociatedEntityMenuItem;
use Drupal\association_menu\AssociationMenuStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Form for editing and adding items to an association menu.
 */
class MenuItemDeleteConfirm extends ConfirmFormBase {

  /**
   * The association the menu item belongs to.
   *
   * @var \Drupal\association\Entity\AssociationInterface
   */
  protected $association;

  /**
   * The menu item being deleted.
   *
   * @var \Drupal\association_menu\MenuItemInterface
   */
  protected $menuItem;

  /**
   * Association menu item storage manager.
   *
   * @var \Drupal\association_menu\AssociationMenuStorageInterface
   */
  protected $menuStorage;

  /**
   * Create a new instance of the association menu delete confirm form.
   *
   * @param \Drupal\association_menu\AssociationMenuStorageInterface $association_menu_storage
   *   Association navigation storage manager.
   */
  public function __construct(AssociationMenuStorageInterface $association_menu_storage) {
    $this->menuStorage = $association_menu_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('association_menu.storage')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'association_menu_delete_item_confirm';
  }

  /**
   * Builds the form elements for the association menu item delete confirm.
   *
   * @param array $form
   *   Current form structure and elements.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state, values and build information.
   * @param \Drupal\association\Entity\AssociationInterface $association
   *   The entity association which this menu item is being deleted.
   * @param int $menu_item_id
   *   The ID of the menu item to delete.
   *
   * @return array
   *   The confirmation form elements for confirming the deletion.
   */
  public function buildForm(array $form, FormStateInterface $form_state, AssociationInterface $association = NULL, $menu_item_id = NULL) {
    try {
      if (!$association || !$menu_item_id) {
        throw new NotFoundHttpException();
      }

      $menuItem = $this->menuStorage->getMenuItem($association, $menu_item_id);

      // Menu items which come from associated entity content,
      // should not be allowed to be deleted here.
      if (!$menuItem || $menuItem instanceof AssociatedEntityMenuItem) {
        throw new AccessDeniedHttpException();
      }
    }
    catch (\InvalidArgumentException $e) {
      throw new NotFoundHttpException();
    }

    // Attach the required menu link data to this form instance.
    $this->association = $association;
    $this->menuItem = $menuItem;

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this->t('Are you sure you want to delete this menu item from @label?', [
      '@label' => $this->association->label(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->t('Menu item %title will be deleted. This action cannot be undone.', [
      '%title' => $this->menuItem->getTitle() ?: '<missing title>',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return Url::fromRoute('association_menu.entity.menu_form', [
      'association' => $this->association->id(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $assoc = $this->association;
    $itemId = $this->menuItem->id();

    if ($this->menuStorage->deleteMenuItem($assoc, $itemId)) {
      $this
        ->messenger()
        ->addStatus($this->t('Successfully removed the menu link %title.', [
          '%title' => $this->menuItem->getTitle(),
        ]));
    }
    else {
      $this
        ->messenger()
        ->addError($this->t('Unable to remove menu link.'));
    }

    $form_state->setRedirectUrl($this->getCancelUrl());
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc