class AccountHeaderElement in Open Social 10.1.x
Same name and namespace in other branches
- 8.9 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
- 8.4 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
- 8.5 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
- 8.6 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
- 8.7 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
- 8.8 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
- 10.3.x modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
- 10.0.x modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
- 10.2.x modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
Provides an account header item element.
Example:
$build["account_block"] = [
"#type" => "account_header_element",
];
Plugin annotation
@RenderElement("account_header_element");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\social_user\Element\AccountHeaderElement
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of AccountHeaderElement
See also
6 #type uses of AccountHeaderElement
- AccountHeaderBlock::build in modules/
social_features/ social_user/ src/ Plugin/ Block/ AccountHeaderBlock.php - Builds and returns the renderable array for this block plugin.
- hook_social_user_account_header_items in modules/
social_features/ social_user/ social_user.api.php - Allows a module to add a link in the account header block.
- LanguageSwitcherBlock::build in modules/
custom/ social_language/ src/ Plugin/ Block/ LanguageSwitcherBlock.php - Builds and returns the renderable array for this block plugin.
- social_activity_social_user_account_header_items in modules/
social_features/ social_activity/ social_activity.module - Implements hook_social_user_account_header_items().
- social_group_social_user_account_header_items in modules/
social_features/ social_group/ social_group.module - Implements hook_social_user_account_header_items().
File
- modules/
social_features/ social_user/ src/ Element/ AccountHeaderElement.php, line 24
Namespace
Drupal\social_user\ElementView source
class AccountHeaderElement extends RenderElement {
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
"#pre_render" => [
[
$class,
"preRenderAccountHeaderElement",
],
],
// The default title attribute for the link.
"#title" => "",
// The default href for the link.
"#url" => Url::fromRoute("<none>"),
// An optional image used in the link (supersedes the icon).
"#image" => NULL,
// An optional icon used in the link.
"#icon" => NULL,
// A label for the link, used on mobile.
"#label" => "",
// The number of notifications for this menu item.
// Will be rendered as a visual indicator if greater than 0.
"#notification_count" => NULL,
// Allows attaching libraries to the account header item.
"#attached" => NULL,
];
}
/**
* Returns an array that can be provided as an item in an item_list.
*
* @param array $item
* The render array for this account header element as defined in getInfo.
*
* @return array
* A render array for an element usable in item_list.
*/
public static function preRenderAccountHeaderElement(array $item) {
// Retrieve the item children, if any, sorted by weight.
$children = Element::children($item, TRUE);
// The link attributes are for the top level link containe in the <li>.
$link_attributes = [];
// The link text is a label with an optional icon or image. If an icon or
// image is used the CSS hides the label for larger screens.
$link_text = [];
// If this link has children then it"s a dropdown.
if (!empty($children)) {
$link_attributes = [
"data-toggle" => "dropdown",
"aria-expanded" => "true",
"aria-haspopup" => "true",
"role" => "button",
"class" => "dropdown-toggle clearfix",
];
}
// We always add the title attribute to the link.
$link_attributes["title"] = $item["#title"];
// Depending on whether an icon, image or title was set. Configure the link.
if (!empty($item["#image"])) {
// The image should be a renderable array so we just add it as link text.
$link_text[] = $item["#image"];
}
elseif (!empty($item["#icon"])) {
// The icon is an SVG icon name without prefix.
$link_text[] = [
"#type" => "inline_template",
"#template" => "<svg class='navbar-nav__icon icon-{{ icon }}'><use xlink:href='#icon-{{ icon }}' /></svg>",
'#context' => [
'icon' => $item['#icon'],
],
];
}
// Allow this menu item to include a notification count.
if ($item['#notification_count'] !== NULL) {
$count_classes = $item['#notification_count'] > 0 ? [
'badge',
'badge-accent',
'badge--pill',
] : [
'sr-only',
];
$link_text[] = [
"#type" => "inline_template",
"#template" => "<span{{ attributes }}>{{ count }}</span>",
'#context' => [
'attributes' => new Attribute([
'class' => $count_classes,
]),
'count' => $item['#notification_count'] > 99 ? "99+" : $item['#notification_count'],
],
];
}
// We always render the label but hide it for non-screenreader users in case
// an image or icon is used.
$label_class = !empty($item['#image']) || !empty($item['#icon']) ? 'sr-only' : NULL;
$link_text[] = [
"#type" => "inline_template",
"#template" => "<span{{ attributes }}>{{ label }}</span>",
'#context' => [
'attributes' => new Attribute([
'class' => [
$label_class,
],
]),
'label' => $item['#label'],
],
];
// If the URL is empty then we use a button instead.
if ($item['#url'] === "") {
// A custom button is rendered because the Drupal built in button element
// is not meant to be used outside of forms.
$element = [
"#type" => "unwrapped_container",
"link" => [
"#type" => "inline_template",
'#template' => "<button {{ attributes }}>{{ label }}</button>",
'#context' => [
"attributes" => new Attribute($link_attributes),
"label" => $link_text,
],
],
];
}
else {
$element = [
"#type" => "unwrapped_container",
"link" => [
"#type" => "link",
"#attributes" => $link_attributes,
"#url" => $item["#url"],
"#title" => $link_text,
],
];
}
// If there are libraries specified, add them to the element.
if (!empty($item['#attached'])) {
$element['#attached'] = $item['#attached'];
}
// If there are children we add them to a sublist.
if (!empty($children)) {
$element["menu_links"] = [
"#theme" => "item_list",
'#list_type' => 'ul',
'#attributes' => [
'class' => [
'dropdown-menu',
],
],
"#items" => array_intersect_key($item, array_flip($children)),
];
}
return $element;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AccountHeaderElement:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
AccountHeaderElement:: |
public static | function | Returns an array that can be provided as an item in an item_list. | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 98 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |