FlagListsFlagLinkBuilder.php in Flag Lists 8
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;
class FlagListsFlagLinkBuilder implements FlagLinkBuilderInterface {
protected $flagLinkBuilder;
public function __construct(FlagLinkBuilderInterface $link_builder) {
$this->flagLinkBuilder = $link_builder;
}
public function build($entity_type_id, $entity_id, $flag_id) {
$flagListsService = \Drupal::service('flaglists');
$templates = $flagListsService
->getAllFlagForList();
$link = [];
foreach ($templates as $template) {
if ($template
->id() == $flag_id) {
return $link;
}
}
$account = \Drupal::currentUser()
->getAccount();
$allFlagLists = $flagListsService
->getAllFlaggingCollections();
$link = $this->flagLinkBuilder
->build($entity_type_id, $entity_id, $flag_id);
foreach ($allFlagLists as $flagList) {
if ($flagList
->getRelatedFlag()
->id() == $flag_id) {
if ($flagList
->access('view', $account)) {
$options = UrlHelper::parse($link['#attributes']['href']);
$uri = 'internal:' . $options['path'] . '/' . $flagList
->id();
unset($options['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);
$link['#flagging_collection'] = $flagList
->id();
}
else {
$link = [];
}
}
}
return $link;
}
}