You are here

public static function AccountHeaderElement::preRenderAccountHeaderElement in Open Social 8.5

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement::preRenderAccountHeaderElement()
  2. 8.4 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement::preRenderAccountHeaderElement()
  3. 8.6 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement::preRenderAccountHeaderElement()
  4. 8.7 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement::preRenderAccountHeaderElement()
  5. 8.8 modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement::preRenderAccountHeaderElement()
  6. 10.3.x modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement::preRenderAccountHeaderElement()
  7. 10.0.x modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement::preRenderAccountHeaderElement()
  8. 10.1.x modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement::preRenderAccountHeaderElement()
  9. 10.2.x modules/social_features/social_user/src/Element/AccountHeaderElement.php \Drupal\social_user\Element\AccountHeaderElement::preRenderAccountHeaderElement()

Returns an array that can be provided as an item in an item_list.

Parameters

array $item: The render array for this account header element as defined in getInfo.

Return value

array A render array for an element usable in item_list.

File

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

Class

AccountHeaderElement
Provides an account header item element.

Namespace

Drupal\social_user\Element

Code

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'],
    ],
  ];
  $element = [
    "#type" => "unwrapped_container",
    "link" => [
      "#type" => "link",
      "#attributes" => $link_attributes,
      "#url" => $item["#url"],
      "#title" => $link_text,
    ],
  ];

  // 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;
}