You are here

function social_user_social_user_account_header_account_links_alter in Open Social 8.4

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_user/social_user.module \social_user_social_user_account_header_account_links_alter()
  2. 8.5 modules/social_features/social_user/social_user.module \social_user_social_user_account_header_account_links_alter()
  3. 8.6 modules/social_features/social_user/social_user.module \social_user_social_user_account_header_account_links_alter()
  4. 8.7 modules/social_features/social_user/social_user.module \social_user_social_user_account_header_account_links_alter()
  5. 8.8 modules/social_features/social_user/social_user.module \social_user_social_user_account_header_account_links_alter()
  6. 10.3.x modules/social_features/social_user/social_user.module \social_user_social_user_account_header_account_links_alter()
  7. 10.0.x modules/social_features/social_user/social_user.module \social_user_social_user_account_header_account_links_alter()
  8. 10.1.x modules/social_features/social_user/social_user.module \social_user_social_user_account_header_account_links_alter()
  9. 10.2.x modules/social_features/social_user/social_user.module \social_user_social_user_account_header_account_links_alter()

Implements hook_social_user_account_header_account_links_alter().

Provides a glue layer between the new system and the deprecated hook social_user_account_header_links to provide backwards compatibility.

This should be removed once social_user_account_header_links is also removed.

File

modules/social_features/social_user/social_user.module, line 445
The social user module alterations.

Code

function social_user_social_user_account_header_account_links_alter(array &$account_links, array $context) {
  $divider = [
    "#wrapper_attributes" => [
      "class" => [
        "divider",
      ],
      "role" => "separator",
    ],
  ];
  $header_links = \Drupal::moduleHandler()
    ->invokeAll('social_user_account_header_links');
  foreach ($header_links as $key => $item) {
    $target = $item['after'];

    // If the item we want to place this after doesn't exist or this item
    // already exists then we skip it.
    if (!isset($account_links[$target]) || isset($account_links[$key])) {
      continue;
    }

    // We try to find a weight for our target.
    $weight = isset($account_links[$target]['#weight']) ? $account_links[$target]['#weight'] : NULL;

    // If our target has no weight we reverse the links array to find an item
    // that does have weight or the end of the array. After which we move back
    // through the array to give everything weights. This allows us to add our
    // element with a weight and avoid complex re-insertion logic while
    // preserving the order of existing items.
    if (is_null($weight)) {
      $order = array_keys($account_links);

      // Pull $i out of the loop to preserve it.
      $i = $order[$target];
      for (; $i >= 0; --$i) {
        if (isset($account_links[$order[$i]]['#weight'])) {
          $weight = $account_links[$order[$i]]['#weight'];

          // Increment $i so we don't overwrite the current element's weight.
          $i++;
          break;
        }
      }

      // If we haven't found a weight we start at the top with 0.
      if (is_null($weight)) {
        $weight = 0;
      }

      // Move back up the stack assigning weights.
      for (; $i <= $order[$target]; $i++) {

        // Increment weight to ensure it's bigger than the previous value. We
        // increment by a larger value to allow room for other insertions.
        $weight += 10;
        $account_links[$order[$i]]['#weight'] = $weight;
      }
    }

    // If this item requests a divider before itself we add it here.
    if (isset($item['divider']) && $item['divider'] === 'before') {
      $account_links[$key . '_divider'] = $divider;
      $account_links[$key . '_divider']['#weight'] = ++$weight;
    }

    // The old implementation of the hook only allowed to add a simple link
    // so we create one here.
    $account_links[$key] = [
      '#type' => 'link',
      '#attributes' => [
        'title' => $item['title'],
      ],
      '#url' => $item['url'],
      '#title' => $item['title'],
      '#weight' => ++$weight,
    ];

    // If this item requests a divider after itself we add it here.
    if (isset($item['divider']) && $item['divider'] === 'after') {
      $account_links[$key . '_divider'] = $divider;
      $account_links[$key . '_divider']['#weight'] = ++$weight;
    }
  }
}