You are here

class FlagListsFlagLinkBuilder in Flag Lists 8

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

Provides a lazy builder for flag lists flag links.

Hierarchy

Expanded class hierarchy of FlagListsFlagLinkBuilder

1 string reference to 'FlagListsFlagLinkBuilder'
flag_lists.services.yml in ./flag_lists.services.yml
flag_lists.services.yml
1 service uses FlagListsFlagLinkBuilder
flag_lists.link_builder in ./flag_lists.services.yml
Drupal\flag_lists\FlagListsFlagLinkBuilder

File

src/FlagListsFlagLinkBuilder.php, line 13

Namespace

Drupal\flag_lists
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlagListsFlagLinkBuilder::$flagLinkBuilder protected property The original flag link builder.
FlagListsFlagLinkBuilder::build public function Lazy builder callback for displaying a flag action link. Overrides FlagLinkBuilderInterface::build
FlagListsFlagLinkBuilder::__construct public function Constructor.