You are here

public function ActionLinkTypeBase::getAsFlagLink in Flag 8.4

Get the action link formatted for use in entity links.

Parameters

\Drupal\flag\FlagInterface $flag: The flag entity.

\Drupal\Core\Entity\EntityInterface $entity: The flaggable entity.

Return value

array The render array.

Overrides ActionLinkTypePluginInterface::getAsFlagLink

2 calls to ActionLinkTypeBase::getAsFlagLink()
AJAXactionLink::getAsFlagLink in src/Plugin/ActionLink/AJAXactionLink.php
Get the action link formatted for use in entity links.
FormEntryTypeBase::getAsFlagLink in src/Plugin/ActionLink/FormEntryTypeBase.php
Get the action link formatted for use in entity links.
2 methods override ActionLinkTypeBase::getAsFlagLink()
AJAXactionLink::getAsFlagLink in src/Plugin/ActionLink/AJAXactionLink.php
Get the action link formatted for use in entity links.
FormEntryTypeBase::getAsFlagLink in src/Plugin/ActionLink/FormEntryTypeBase.php
Get the action link formatted for use in entity links.

File

src/ActionLink/ActionLinkTypeBase.php, line 97

Class

ActionLinkTypeBase
Provides a base class for all link types.

Namespace

Drupal\flag\ActionLink

Code

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;
}