You are here

public function AddressBookController::overviewPage in Commerce Core 8.2

Builds the overview page.

Parameters

\Drupal\user\UserInterface $user: The user account.

Return value

array The response.

1 string reference to 'AddressBookController::overviewPage'
commerce_order.routing.yml in modules/order/commerce_order.routing.yml
modules/order/commerce_order.routing.yml

File

modules/order/src/Controller/AddressBookController.php, line 138

Class

AddressBookController
Provides the address book UI.

Namespace

Drupal\commerce_order\Controller

Code

public function overviewPage(UserInterface $user) {
  $profile_types = $this->addressBook
    ->loadTypes();
  $profile_types = $this
    ->filterTypesByViewAccess($profile_types, $user);
  $profile_entity_type = $this->entityTypeManager
    ->getDefinition('profile');
  $view_builder = $this->entityTypeManager
    ->getViewBuilder('profile');
  $wrapper_element_type = count($profile_types) > 1 ? 'details' : 'container';
  $cacheability = new CacheableMetadata();
  $cacheability
    ->addCacheContexts($profile_entity_type
    ->getListCacheContexts());
  $cacheability
    ->addCacheTags($profile_entity_type
    ->getListCacheTags());
  $build = [];
  $build['#attached']['library'][] = 'commerce_order/address_book';
  foreach ($profile_types as $profile_type_id => $profile_type) {
    $add_form_url = Url::fromRoute('commerce_order.address_book.add_form', [
      'user' => $user
        ->id(),
      'profile_type' => $profile_type
        ->id(),
    ]);
    if ($profile_type
      ->allowsMultiple()) {
      $profiles = $this->addressBook
        ->loadAll($user, $profile_type_id);
    }
    else {
      $profile = $this->addressBook
        ->load($user, $profile_type_id);
      $profiles = [];
      if ($profile) {
        $profiles[$profile
          ->id()] = $profile;
      }
    }
    $build[$profile_type_id] = [
      '#type' => $wrapper_element_type,
      '#title' => $profile_type
        ->getDisplayLabel() ?: $profile_type
        ->label(),
      '#open' => TRUE,
      '#attributes' => [
        'class' => [
          'address-book__container',
          'address-book__container--' . $profile_type_id,
        ],
      ],
    ];
    $build[$profile_type_id]['add'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Add address'),
      '#url' => $add_form_url,
      '#attributes' => [
        'class' => [
          'address-book__add-link',
        ],
      ],
      '#access' => $add_form_url
        ->access(),
    ];
    $build[$profile_type_id]['profiles'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'address-book__profiles',
        ],
      ],
      '#access' => !empty($profiles),
    ];
    foreach ($profiles as $profile_id => $profile) {
      $build[$profile_type_id]['profiles'][$profile_id] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'address-book__profile',
          ],
        ],
        'profile' => $view_builder
          ->view($profile),
        'operations' => $this
          ->buildOperations($profile, $profile_type),
      ];

      // Allow default profiles to be styled differently.
      if ($profile
        ->isDefault()) {
        $build[$profile_type_id]['profiles'][$profile_id]['#attributes']['class'][] = 'address-book__profile--default';
      }
    }
    if (empty($profiles)) {
      $build[$profile_type_id]['empty_text'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'address-book__empty-text',
          ],
        ],
        '#plain_text' => $this
          ->t('There are no addresses yet.'),
        // If the wrapper is a fieldset and there's an add link, the
        // empty text is redundant.
        '#access' => $wrapper_element_type == 'container' || !$add_form_url
          ->access(),
      ];
    }
    $cacheability
      ->addCacheableDependency($profile_type);
  }
  $cacheability
    ->applyTo($build);
  return $build;
}