FlagListsFlagLinkBuilder.php in Flag Lists 4.0.x
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;
use Drupal\Core\Security\TrustedCallbackInterface;
class FlagListsFlagLinkBuilder implements FlagLinkBuilderInterface, TrustedCallbackInterface {
protected $flagLinkBuilder;
protected $flagListsService;
public function __construct(FlagLinkBuilderInterface $link_builder, FlagListsServiceInterface $flag_lists_service) {
$this->flagLinkBuilder = $link_builder;
$this->flagListsService = $flag_lists_service;
}
public static function trustedCallbacks() {
return [
'build',
];
}
public function build($entity_type_id, $entity_id, $flag_id) {
$templates = $this->flagListsService
->getAllFlagForList();
$link = [];
foreach ($templates as $template) {
if ($template
->id() == $flag_id) {
return $link;
}
}
$account = \Drupal::currentUser()
->getAccount();
$allFlagLists = $this->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) && $flagList
->getOwnerId() == $account
->id()) {
$options = UrlHelper::parse($link['#attributes']['href']);
$uri = substr_replace($options['path'], 'internal:/', 0, strlen(base_path())) . '/' . $flagList
->id();
unset($options['path']);
unset($options['query']['token']);
$rendered_url = Url::fromUri($uri, $options)
->toString(TRUE);
$rendered_url
->applyTo($link);
$link['#attributes']['href'] = $rendered_url
->getGeneratedUrl();
$flc = $this->flagListsService
->getFlaggingCollectionById($flagList
->id());
$token_service = \Drupal::token();
$bubbleable_metadata = BubbleableMetadata::createFromRenderArray($link);
$link['#title']['#markup'] = $token_service
->replace($link['#title']['#markup'], [
'flagging_collection' => $flc,
], [], $bubbleable_metadata);
$link['#attributes']['title'] = $token_service
->replace($link['#attributes']['title'], [
'flagging_collection' => $flc,
], [], $bubbleable_metadata);
$bubbleable_metadata
->applyTo($link);
$link['#flagging_collection'] = $flagList
->id();
}
else {
$link = [];
}
}
}
return $link;
}
}