You are here

public function PanelizerUser::alterBuild in Panelizer 8.4

Same name and namespace in other branches
  1. 8.5 src/Plugin/PanelizerEntity/PanelizerUser.php \Drupal\panelizer\Plugin\PanelizerEntity\PanelizerUser::alterBuild()
  2. 8.3 src/Plugin/PanelizerEntity/PanelizerUser.php \Drupal\panelizer\Plugin\PanelizerEntity\PanelizerUser::alterBuild()

Alter the built entity view in an entity specific way before rendering.

This is useful for adding things like contextual links.

Parameters

array $build: The render array that is being created.

\Drupal\Core\Entity\EntityInterface $entity: The entity to be prepared.

\Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display: The Panels display used to render this entity.

string $view_mode: The view mode that should be used to prepare the entity.

Overrides PanelizerEntityBase::alterBuild

File

src/Plugin/PanelizerEntity/PanelizerUser.php, line 45

Class

PanelizerUser
Panelizer entity plugin for integrating with users.

Namespace

Drupal\panelizer\Plugin\PanelizerEntity

Code

public function alterBuild(array &$build, EntityInterface $entity, PanelsDisplayVariant $panels_display, $view_mode) {

  /** @var $entity \Drupal\user\Entity\User */
  parent::alterBuild($build, $entity, $panels_display, $view_mode);
  if ($entity
    ->id()) {
    $build['#contextual_links']['user'] = [
      'route_parameters' => [
        'user' => $entity
          ->id(),
      ],
      'metadata' => [
        'changed' => $entity
          ->getChangedTime(),
      ],
    ];
  }

  // This function adds a default alt tag to the user_picture field to
  // maintain accessibility.
  if (user_picture_enabled() && !empty($build['content']['content'])) {
    foreach (Element::children($build['content']['content']) as $key) {
      if (isset($build['content']['content'][$key]['content']['field'])) {
        foreach (Element::children($build['content']['content'][$key]['content']['field']) as $field_key) {
          if ($build['content']['content'][$key]['content']['field']['#field_name'] == 'user_picture') {
            if (empty($build['content']['content'][$key]['content']['field'][$field_key]['#item_attributes'])) {
              $build['content']['content'][$key]['content']['field'][$field_key]['#item_attributes'] = [
                'alt' => \Drupal::translation()
                  ->translate('Profile picture for user @username', [
                  '@username' => $entity
                    ->getUsername(),
                ]),
              ];
            }
          }
        }
      }
    }
  }
}