class FlagListsFlagLinkBuilder in Flag Lists 8
Same name and namespace in other branches
- 4.0.x src/FlagListsFlagLinkBuilder.php \Drupal\flag_lists\FlagListsFlagLinkBuilder
Provides a lazy builder for flag lists flag links.
Hierarchy
- class \Drupal\flag_lists\FlagListsFlagLinkBuilder implements FlagLinkBuilderInterface
Expanded class hierarchy of FlagListsFlagLinkBuilder
1 string reference to 'FlagListsFlagLinkBuilder'
1 service uses FlagListsFlagLinkBuilder
File
- src/
FlagListsFlagLinkBuilder.php, line 13
Namespace
Drupal\flag_listsView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FlagListsFlagLinkBuilder:: |
protected | property | The original flag link builder. | |
FlagListsFlagLinkBuilder:: |
public | function |
Lazy builder callback for displaying a flag action link. Overrides FlagLinkBuilderInterface:: |
|
FlagListsFlagLinkBuilder:: |
public | function | Constructor. |