public function GroupOperationsBlock::build in Group 2.0.x
Same name and namespace in other branches
- 8 src/Plugin/Block/GroupOperationsBlock.php \Drupal\group\Plugin\Block\GroupOperationsBlock::build()
Builds and returns the renderable array for this block plugin.
If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).
Return value
array A renderable array representing the content of the block.
Overrides BlockPluginInterface::build
See also
\Drupal\block\BlockViewBuilder
File
- src/
Plugin/ Block/ GroupOperationsBlock.php, line 24
Class
- GroupOperationsBlock
- Provides a block with operations the user can perform on a group.
Namespace
Drupal\group\Plugin\BlockCode
public function build() {
$build = [];
// The operations available in this block vary per the current user's group
// permissions. It obviously also varies per group, but we cannot know for
// sure how we got that group as it is up to the context provider to
// implement that. This block will then inherit the appropriate cacheable
// metadata from the context, as set by the context provider.
$cacheable_metadata = new CacheableMetadata();
$cacheable_metadata
->setCacheContexts([
'user.group_permissions',
]);
/** @var \Drupal\group\Entity\GroupInterface $group */
if (($group = $this
->getContextValue('group')) && $group
->id()) {
$links = [];
// Retrieve the operations and cacheable metadata from the installed
// content plugins.
foreach ($group
->getGroupType()
->getInstalledContentPlugins() as $plugin) {
/** @var \Drupal\group\Plugin\Group\Relation\GroupRelationInterface $plugin */
$links += $plugin
->getGroupOperations($group);
$cacheable_metadata = $cacheable_metadata
->merge($plugin
->getGroupOperationsCacheableMetadata());
}
if ($links) {
// Allow modules to alter the collection of gathered links.
\Drupal::moduleHandler()
->alter('group_operations', $links, $group);
// Sort the operations by weight.
uasort($links, '\\Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
// Create an operations element with all of the links.
$build['#type'] = 'operations';
$build['#links'] = $links;
}
}
// Set the cacheable metadata on the build.
$cacheable_metadata
->applyTo($build);
return $build;
}