You are here

abstract class ActionLinkTypeBase in Flag 8.4

Provides a base class for all link types.

Link types perform two key functions within Flag: They specify the route to use when a flag link is clicked, and generate the render array to display flag links.

Hierarchy

Expanded class hierarchy of ActionLinkTypeBase

2 files declare their use of ActionLinkTypeBase
FormEntryTypeBase.php in src/Plugin/ActionLink/FormEntryTypeBase.php
Reload.php in src/Plugin/ActionLink/Reload.php

File

src/ActionLink/ActionLinkTypeBase.php, line 25

Namespace

Drupal\flag\ActionLink
View source
abstract class ActionLinkTypeBase extends PluginBase implements ActionLinkTypePluginInterface, ContainerFactoryPluginInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;
  use StringTranslationTrait;
  use RedirectDestinationTrait;

  /**
   * Build a new link type instance and sets the configuration.
   *
   * @param array $configuration
   *   The configuration array with which to initialize this plugin.
   * @param string $plugin_id
   *   The ID with which to initialize this plugin.
   * @param array $plugin_definition
   *   The plugin definition array.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, AccountInterface $current_user) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configuration += $this
      ->defaultConfiguration();
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('current_user'));
  }

  /**
   * Return a Url object for the given flag action.
   *
   * @param string $action
   *   The action, flag or unflag.
   * @param \Drupal\flag\FlagInterface $flag
   *   The flag entity.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The flaggable entity.
   *
   * @return Url
   *   The Url object for this plugin's flag/unflag route.
   */
  protected abstract function getUrl($action, FlagInterface $flag, EntityInterface $entity);

  /**
   * {@inheritdoc}
   */
  public function getAsLink(FlagInterface $flag, EntityInterface $entity) {
    $action = $this
      ->getAction($flag, $entity);
    $url = $this
      ->getUrl($action, $flag, $entity);
    $url
      ->setOption('query', [
      'destination' => $this
        ->getDestination(),
    ]);
    $title = $flag
      ->getShortText($action);
    return Link::fromTextAndUrl($title, $url);
  }

  /**
   * {@inheritdoc}
   */
  public function getAsFlagLink(FlagInterface $flag, EntityInterface $entity) {
    $action = $this
      ->getAction($flag, $entity);
    $access = $flag
      ->actionAccess($action, $this->currentUser, $entity);
    if ($access
      ->isAllowed()) {
      $url = $this
        ->getUrl($action, $flag, $entity);
      $url
        ->setRouteParameter('destination', $this
        ->getDestination());
      $render = [
        '#theme' => 'flag',
        '#flag' => $flag,
        '#flaggable' => $entity,
        '#action' => $action,
        '#access' => $access
          ->isAllowed(),
        // Use render array for title to allow limited markup in the link text.
        '#title' => [
          '#markup' => $flag
            ->getShortText($action),
        ],
        '#attributes' => [
          'title' => $flag
            ->getLongText($action),
        ],
      ];

      // Build the URL. It is important that bubbleable metadata is explicitly
      // collected and applied to the render array, as it might be rendered on
      // its own, for example in an ajax response. Specifically, this is
      // necessary for CSRF token placeholder replacements.
      $rendered_url = $url
        ->toString(TRUE);
      $rendered_url
        ->applyTo($render);
      $render['#attributes']['href'] = $rendered_url
        ->getGeneratedUrl();
    }
    else {
      $render = [];
    }
    CacheableMetadata::createFromRenderArray($render)
      ->addCacheableDependency($access)
      ->applyTo($render);
    return $render;
  }

  /**
   * Helper method to get the next flag action the user can take.
   *
   * @param string $action
   *   The action, flag or unflag.
   * @param \Drupal\flag\FlagInterface $flag
   *   The flag entity.
   *
   * @return string
   */
  protected function getAction(FlagInterface $flag, EntityInterface $entity) {
    return $flag
      ->isFlagged($entity) ? 'unflag' : 'flag';
  }

  /**
   * Helper method to generate a destination URL parameter.
   *
   * @return string
   *  A string containing a destination URL parameter.
   */
  protected function getDestination() {
    return $this
      ->getRedirectDestination()
      ->get();
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return [];
  }

  /**
   * Provides a form array for the action link plugin's settings form.
   *
   * Derived classes will want to override this method.
   *
   * @param array $form
   *   The form array.
   * @param FormStateInterface $form_state
   *   The form state.
   *
   * @return array
   *   The modified form array.
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    return $form;
  }

  /**
   * Processes the action link setting form submit.
   *
   * Derived classes will want to override this method.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {

    // Override this.
  }

  /**
   * Validates the action link setting form.
   *
   * Derived classes will want to override this method.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {

    // Override this.
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = NestedArray::mergeDeep($this
      ->defaultConfiguration(), $configuration);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActionLinkTypeBase::$currentUser protected property The current user.
ActionLinkTypeBase::buildConfigurationForm public function Provides a form array for the action link plugin's settings form. Overrides PluginFormInterface::buildConfigurationForm 1
ActionLinkTypeBase::calculateDependencies public function
ActionLinkTypeBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
ActionLinkTypeBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 1
ActionLinkTypeBase::getAction protected function Helper method to get the next flag action the user can take.
ActionLinkTypeBase::getAsFlagLink public function Get the action link formatted for use in entity links. Overrides ActionLinkTypePluginInterface::getAsFlagLink 2
ActionLinkTypeBase::getAsLink public function Get the action link as a Link object. Overrides ActionLinkTypePluginInterface::getAsLink
ActionLinkTypeBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ActionLinkTypeBase::getDestination protected function Helper method to generate a destination URL parameter. 1
ActionLinkTypeBase::getUrl abstract protected function Return a Url object for the given flag action. 3
ActionLinkTypeBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ActionLinkTypeBase::submitConfigurationForm public function Processes the action link setting form submit. Overrides PluginFormInterface::submitConfigurationForm 1
ActionLinkTypeBase::validateConfigurationForm public function Validates the action link setting form. Overrides PluginFormInterface::validateConfigurationForm 1
ActionLinkTypeBase::__construct public function Build a new link type instance and sets the configuration. Overrides PluginBase::__construct 1
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.