You are here

class AccountHeaderElement in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
  2. 8.4 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
  3. 8.5 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
  4. 8.6 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
  5. 8.7 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
  6. 8.8 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
  7. 10.3.x modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
  8. 10.0.x modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement
  9. 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

Expanded class hierarchy of AccountHeaderElement

See also

Plugin API

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().

... See full list

File

modules/social_features/social_user/src/Element/AccountHeaderElement.php, line 24

Namespace

Drupal\social_user\Element
View 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

Namesort descending Modifiers Type Description Overrides
AccountHeaderElement::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
AccountHeaderElement::preRenderAccountHeaderElement public static function Returns an array that can be provided as an item in an item_list.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.