You are here

FlagListsFlagLinkBuilder.php in Flag Lists 8

Same filename and directory in other branches
  1. 4.0.x src/FlagListsFlagLinkBuilder.php

Namespace

Drupal\flag_lists

File

src/FlagListsFlagLinkBuilder.php
View source
<?php

namespace Drupal\flag_lists;

use Drupal\flag\FlagLinkBuilderInterface;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Url;
use Drupal\Core\Render\BubbleableMetadata;

/**
 * Provides a lazy builder for flag lists flag links.
 */
class FlagListsFlagLinkBuilder implements FlagLinkBuilderInterface {

  /**
   * The original flag link builder.
   *
   * @var \Drupal\flag\FlagLinkBuilder
   */
  protected $flagLinkBuilder;

  /**
   * Constructor.
   *
   * @param \Drupal\flag\FlagLinkBuilderInterface $link_builder
   *   The original FlagLinkBuilder.
   */
  public function __construct(FlagLinkBuilderInterface $link_builder) {
    $this->flagLinkBuilder = $link_builder;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Classes

Namesort descending Description
FlagListsFlagLinkBuilder Provides a lazy builder for flag lists flag links.