You are here

public function FlagListsFlagLinkBuilder::build in Flag Lists 8

Same name and namespace in other branches
  1. 4.0.x src/FlagListsFlagLinkBuilder.php \Drupal\flag_lists\FlagListsFlagLinkBuilder::build()

Lazy builder callback for displaying a flag action link.

Parameters

string $entity_type_id: The entity type ID for which the link should be shown.

string|int $entity_id: The entity ID for which the link should be shown.

string $flag_id: The flag ID for which the link should be shown.

Return value

array A render array for the action link, empty if the user does not have access.

Overrides FlagLinkBuilderInterface::build

File

src/FlagListsFlagLinkBuilder.php, line 35

Class

FlagListsFlagLinkBuilder
Provides a lazy builder for flag lists flag links.

Namespace

Drupal\flag_lists

Code

public function build($entity_type_id, $entity_id, $flag_id) {

  // $entity_type_id = 'node' etc
  // $entity_id 'node id' etc
  // $flag_id 'flag' machine name
  $flagListsService = \Drupal::service('flaglists');
  $templates = $flagListsService
    ->getAllFlagForList();
  $link = [];
  foreach ($templates as $template) {
    if ($template
      ->id() == $flag_id) {

      // Don't return a link to a template.
      // (All flag lists are actually real flags.)
      return $link;
    }
  }
  $account = \Drupal::currentUser()
    ->getAccount();
  $allFlagLists = $flagListsService
    ->getAllFlaggingCollections();

  // Build a link according to original scheme.
  $link = $this->flagLinkBuilder
    ->build($entity_type_id, $entity_id, $flag_id);

  // Walk through all flag lists.
  // Update only links of flag lists.
  foreach ($allFlagLists as $flagList) {

    // If this is the specific we are looking for.
    if ($flagList
      ->getRelatedFlag()
      ->id() == $flag_id) {

      // Better to check for access than ownership.
      if ($flagList
        ->access('view', $account)) {

        // Build a flag link with flag method to check the access.
        $options = UrlHelper::parse($link['#attributes']['href']);
        $uri = 'internal:' . $options['path'] . '/' . $flagList
          ->id();
        unset($options['path']);

        // Need to remove the token in order to regenerate it
        // for the new path.
        unset($options['query']['token']);
        $link['#attributes']['href'] = Url::fromUri($uri, $options)
          ->toString();
        $flagListService = \Drupal::service('flaglists');
        $flc = $flagListService
          ->getFlaggingCollectionById($flagList
          ->id());
        $token_service = \Drupal::token();
        $bubbleable_metadata = new BubbleableMetadata();
        $link['#title']['#markup'] = $token_service
          ->replace($link['#title']['#markup'], [
          'flagging_collection' => $flc,
        ], [], $bubbleable_metadata);
        $bubbleable_metadata
          ->applyTo($link);

        // Make flagging collection available in the twig.
        $link['#flagging_collection'] = $flagList
          ->id();
      }
      else {
        $link = [];
      }
    }
  }
  return $link;
}