flag_lists-4.0.x-dev/src/Controller/ActionLinkNoJsController.php

src/Controller/ActionLinkNoJsController.php
<?php

namespace Drupal\flag_lists\Controller;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\flag\FlagInterface;
use Drupal\flag\FlagServiceInterface;
use Drupal\flag_lists\FlagListsServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * Returns nojs responses to flag and unflag action links.
 *
 * "nojs" is when the user agent has javascript disabled the
 * behaviour reverts to that of a normal link.
 *
 * After an update the response to a valid request is a redirect to the entity
 * with drupal update message.
 */
class ActionLinkNoJsController implements ContainerInjectionInterface {
  /**
   * The flag service.
   *
   * @var \Drupal\flag\FlagServiceInterface
   */
  protected $flagService;

  /**
   * The flag lists service.
   *
   * @var \Drupal\flag_lists\FlagListsServiceInterface
   */
  protected $flagListsService;

  /**
   * The messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Constructor.
   *
   * @param \Drupal\flag\FlagServiceInterface $flag
   *   The flag service.
   * @param \Drupal\flag_lists\FlagListsServiceInterface $flag_lists
   *   The flag lists service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   */
  public function __construct(
    FlagServiceInterface $flag,
    FlagListsServiceInterface $flag_lists,
    MessengerInterface $messenger) {
    $this->flagService = $flag;
    $this->flagListsService = $flag_lists;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('flag'),
      $container->get('flaglists'),
      $container->get('messenger')
    );
  }

  /**
   * Performs a flagging when called via a route.
   *
   * @param \Drupal\flag\FlagInterface $flag
   *   The flag entity.
   * @param int $entity_id
   *   The flaggable entity ID.
   * @param string $view_mode
   *   The view mode to be used.
   * @param string $flag_list
   *   The flag list from the link.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
   *   The response object, only if successful.
   *
   * @see \Drupal\flag\Plugin\Reload
   */
  public function flag(FlagInterface $flag, $entity_id, $view_mode, $flag_list) {
    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $this->flagService->getFlaggableById($flag, $entity_id);

    try {
      $this->flagService->flag($flag, $entity);
    }
    catch (\LogicException $e) {
      // Fail silently so we return to the entity, which will show an updated
      // link for the existing state of the flag.
    }

    // Create the flag list item.
    $actionLinkHelper = new ActionLinkHelper($this->flagListsService);
    $actionLinkHelper->flagHelper($flag, $entity_id, $flag_list);

    return $this->generateResponse($entity, $flag->getMessage('flag'));
  }

  /**
   * Performs a unflagging when called via a route.
   *
   * @param \Drupal\flag\FlagInterface $flag
   *   The flag entity.
   * @param int $entity_id
   *   The flaggable entity ID.
   * @param string $view_mode
   *   The view mode to be used.
   * @param string $flag_list
   *   The flag list from the link.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
   *   The response object, only if successful.
   *
   * @see \Drupal\flag\Plugin\Reload
   */
  public function unflag(FlagInterface $flag, $entity_id, $view_mode, $flag_list) {
    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $this->flagService->getFlaggableById($flag, $entity_id);

    try {
      $this->flagService->unflag($flag, $entity);
    }
    catch (\LogicException $e) {
      // Fail silently so we return to the entity, which will show an updated
      // link for the existing state of the flag.
    }

    // Remove the flag list item.
    $actionLinkHelper = new ActionLinkHelper($this->flagListsService);
    $actionLinkHelper->unflagHelper($flag, $entity_id, $flag_list);

    return $this->generateResponse($entity, $flag->getMessage('unflag'));
  }

  /**
   * Generates a response after the flag has been updated.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity object.
   * @param string $message
   *   The message to display.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   The response object.
   */
  private function generateResponse(EntityInterface $entity, $message) {
    $this->messenger->addMessage($message);

    if ($entity->hasLinkTemplate('canonical')) {
      // Redirect back to the entity. A passed in destination query parameter
      // will automatically override this.
      $url_info = $entity->toUrl();

      $options['absolute'] = TRUE;
      $url = Url::fromRoute($url_info->getRouteName(), $url_info->getRouteParameters(), $options);
      $response = new RedirectResponse($url->toString());
    }
    else {
      // For entities that don't have a canonical URL (like paragraphs),
      // redirect to the front page.
      $front = Url::fromUri('internal:/');
      $response = new RedirectResponse($front);
    }

    return $response;
  }

}

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

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