You are here

protected function AccountHeaderBlock::hasAccessibleChild in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_user/src/Plugin/Block/AccountHeaderBlock.php \Drupal\social_user\Plugin\Block\AccountHeaderBlock::hasAccessibleChild()
  2. 10.3.x modules/social_features/social_user/src/Plugin/Block/AccountHeaderBlock.php \Drupal\social_user\Plugin\Block\AccountHeaderBlock::hasAccessibleChild()
  3. 10.0.x modules/social_features/social_user/src/Plugin/Block/AccountHeaderBlock.php \Drupal\social_user\Plugin\Block\AccountHeaderBlock::hasAccessibleChild()
  4. 10.1.x modules/social_features/social_user/src/Plugin/Block/AccountHeaderBlock.php \Drupal\social_user\Plugin\Block\AccountHeaderBlock::hasAccessibleChild()
  5. 10.2.x modules/social_features/social_user/src/Plugin/Block/AccountHeaderBlock.php \Drupal\social_user\Plugin\Block\AccountHeaderBlock::hasAccessibleChild()

Checks if the element has a child with a valid access check.

Loops through the children until it finds an element whose access is set to true or who has an access check returning true.

This method is needed in case the parent creates markup that should be hidden if the user can't access any of the children.

This method mutates the checked children and sets their #access values so that the access checks don't have to happen multiple times.

Parameters

array $parent: The parent element whose children to check.

Return value

bool Whether the parent has visible children.

1 call to AccountHeaderBlock::hasAccessibleChild()
AccountHeaderBlock::build in modules/social_features/social_user/src/Plugin/Block/AccountHeaderBlock.php
Builds and returns the renderable array for this block plugin.

File

modules/social_features/social_user/src/Plugin/Block/AccountHeaderBlock.php, line 303

Class

AccountHeaderBlock
Provides a 'AccountHeaderBlock' block.

Namespace

Drupal\social_user\Plugin\Block

Code

protected function hasAccessibleChild(array &$parent) : bool {
  $children = Element::children($parent);
  foreach ($children as $key) {
    $element =& $parent[$key];

    // If there's no precomputed access value, check for access callbacks.
    // This is logic adapted from Drupal\Core\Render\Renderer::doRender.
    if (!empty($element) && !isset($element['#access']) && isset($element['#access_callback'])) {
      if (is_string($element['#access_callback']) && strpos($element['#access_callback'], '::') === FALSE) {
        $element['#access_callback'] = $this->controllerResolver
          ->getControllerFromDefinition($element['#access_callback']);
      }

      // Store the result of the access callback so it can be checked.
      $element['#access'] = call_user_func($element['#access_callback'], $element);
    }

    // Check if there's a precomputed access value or if one has been set
    // from an access callback.
    if (isset($element['#access'])) {

      // It can either be an access result or a true/false value.
      if ($element['#access'] instanceof AccessResultInterface) {

        // If this child is inaccessible then we check the other children.
        if ($element['#access']
          ->isForbidden()) {
          continue;
        }

        // If the child is not forbidden then we've found an accessible child.
        return TRUE;
      }

      // If the access variable is falsy then check the other children.
      if (!$element['#access']) {
        continue;
      }

      // A truthy access value has been found so we have an accessible child.
      return TRUE;
    }

    // If this element has no access checks then it's visible.
    return TRUE;
  }

  // No accessible child was found.
  return FALSE;
}