You are here

class AppListBuilder in Apigee Edge 8

General app list builder for developer and team apps.

Hierarchy

Expanded class hierarchy of AppListBuilder

2 files declare their use of AppListBuilder
TeamAppListBuilder.php in modules/apigee_edge_teams/src/Entity/ListBuilder/TeamAppListBuilder.php
TeamAppListByTeam.php in modules/apigee_edge_teams/src/Entity/ListBuilder/TeamAppListByTeam.php

File

src/Entity/ListBuilder/AppListBuilder.php, line 41

Namespace

Drupal\apigee_edge\Entity\ListBuilder
View source
class AppListBuilder extends EdgeEntityListBuilder {

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The app warnings checker.
   *
   * @var \Drupal\apigee_edge\Entity\AppWarningsCheckerInterface
   */
  protected $appWarningsChecker;

  /**
   * AppListBuilder constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   *   The time service.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack object.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface|null $config_factory
   *   The config factory.
   * @param \Drupal\apigee_edge\Entity\AppWarningsCheckerInterface $app_warnings_checker
   *   The app warnings checker service.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, RequestStack $request_stack, TimeInterface $time, ConfigFactoryInterface $config_factory = NULL, AppWarningsCheckerInterface $app_warnings_checker = NULL) {
    if (!$config_factory) {
      $config_factory = \Drupal::service('config.factory');
    }
    if (!$app_warnings_checker) {
      @trigger_error('Calling ' . __METHOD__ . ' without the $app_warnings_checker is deprecated in apigee_edge:8-x-1.18 and is required before apigee_edge:8.x-2.0. See https://github.com/apigee/apigee-edge-drupal/pull/507', E_USER_DEPRECATED);
      $app_warnings_checker = \Drupal::service('apigee_edge.entity.app_warnings_checker');
    }
    parent::__construct($entity_type, $entity_type_manager, $config_factory);
    $this->renderer = $renderer;
    $this->entityTypeManager = $entity_type_manager;
    $this->requestStack = $request_stack;
    $this->time = $time;
    $this->appWarningsChecker = $app_warnings_checker;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('entity_type.manager'), $container
      ->get('renderer'), $container
      ->get('request_stack'), $container
      ->get('datetime.time'), $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  protected function getDefaultOperations(EntityInterface $entity) {
    $operations = parent::getDefaultOperations($entity);
    if ($entity
      ->access('analytics') && $entity
      ->hasLinkTemplate('analytics')) {
      $operations['analytics'] = [
        'title' => $this
          ->t('Analytics'),
        'weight' => 150,
        'url' => $entity
          ->toUrl('analytics'),
      ];
    }
    return $operations;
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $headers = [];
    $headers['name'] = [
      'data' => $this
        ->t('@entity_type name', [
        '@entity_type' => ucfirst($this->entityType
          ->getSingularLabel()),
      ]),
      'specifier' => 'displayName',
      'field' => 'displayName',
      'sort' => 'asc',
    ];
    $headers['status'] = [
      'data' => $this
        ->t('Status'),
      'specifier' => 'status',
      'field' => 'status',
    ];
    return $headers + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {

    // We may return multiple rows for one entity which is not supported by
    // the parent class. (See render().)
    return [];
  }

  /**
   * Builds an info row for an app in the entity listing.
   *
   * The info row contains the app's name (link to the details page),
   * status and entity operations.
   *
   * @param \Drupal\apigee_edge\Entity\AppInterface $app
   *   The app entity.
   * @param array $rows
   *   The info row in the table for app.
   */
  protected function buildInfoRow(AppInterface $app, array &$rows) {
    $css_id = $this
      ->getCssIdForInfoRow($app);
    $row = [
      'data' => [],
      'id' => $css_id,
      'class' => [
        'row--app',
        'row--info',
      ],
    ];
    $row['data']['name']['data'] = $this
      ->renderAppName($app);
    $row['data']['status']['data'] = $this
      ->renderAppStatus($app);
    $row['data'] += parent::buildRow($app);

    // Allow child classes to add items to the beginning of a row.
    if (array_key_exists($css_id, $rows)) {
      $rows[$css_id] = NestedArray::mergeDeep($rows[$css_id], $row);
    }
    else {
      $rows[$css_id] = $row;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $build = parent::render();

    // Add user cache contexts.
    $build['#cache']['contexts'][] = 'user';
    $build['#cache']['contexts'][] = 'user.permissions';
    if ($this
      ->usingDisplayType(static::VIEW_MODE_DISPLAY_TYPE)) {
      return $build;
    }
    $build['table']['#attributes']['class'][] = 'table--app-list';
    $build['table']['#rows'] = [];
    $build['table']['#empty'] = $this
      ->t('Looks like you do not have any apps. Get started by adding one.');
    foreach ($this
      ->load() as $entity) {
      $rows = [];
      $this
        ->buildInfoRow($entity, $rows);
      $this
        ->buildWarningRow($entity, $rows);
      $build['table']['#rows'] += $rows;
    }
    $build['#attached']['library'][] = 'apigee_edge/apigee_edge.app_listing';
    return $build;
  }

  /**
   * Builds a warning row for an app in the entity listing.
   *
   * The warning row contains the warning messages if there is any.
   *
   * @param \Drupal\apigee_edge\Entity\AppInterface $app
   *   The app entity.
   * @param array $rows
   *   The warning row in the table for app.
   */
  protected function buildWarningRow(AppInterface $app, array &$rows) {
    $info_row_css_id = $this
      ->getCssIdForInfoRow($app);
    $warning_row_css_id = $this
      ->getCssIdForWarningRow($app);
    $row = [
      'data' => [],
      'id' => $warning_row_css_id,
      'class' => [
        'row--app',
        'row--warning',
      ],
    ];
    $warnings = $this
      ->checkAppCredentialWarnings($app);

    // Display warning sign next to the status if the app's status is
    // approved, but:
    // - any credentials of the app is in revoked or expired status
    // - any products of any credentials of the app is in revoked or
    //   pending status.
    if ($app
      ->getStatus() === AppInterface::STATUS_APPROVED && ($warnings['revokedCred'] || $warnings['revokedOrPendingCredProduct'] || $warnings['expiredCred'])) {
      $build['status'] = $rows[$info_row_css_id]['data']['status']['data'];
      $build['warning'] = [
        '#type' => 'html_tag',
        '#tag' => 'span',
        '#value' => '!',
        '#attributes' => [
          'class' => 'circle',
        ],
      ];
      $link_options = [
        'attributes' => [
          'class' => [
            'toggle--warning',
            'closed',
          ],
          'data-text-open' => [
            $this
              ->t('Show details'),
          ],
          'data-text-closed' => [
            $this
              ->t('Hide details'),
          ],
        ],
        'fragment' => $warning_row_css_id,
      ];
      $url = Url::fromUserInput($this->requestStack
        ->getCurrentRequest()
        ->getRequestUri(), $link_options);
      $link = Link::fromTextAndUrl($this
        ->t('<span class="ui-icon-triangle-1-e ui-icon"></span><span class="text">Show details</span>'), $url);
      $build['warning-toggle'] = $link
        ->toRenderable();
      $rows[$info_row_css_id]['data']['status']['data'] = $this->renderer
        ->renderPlain($build);
      $row['data']['info'] = [
        'colspan' => count($this
          ->buildHeader()),
      ];
      $items = [];
      if ($warnings['revokedCred']) {
        $items[] = $warnings['revokedCred'];
      }
      elseif ($warnings['revokedOrPendingCredProduct']) {
        $items[] = $warnings['revokedOrPendingCredProduct'];
      }
      if ($warnings['expiredCred']) {
        $items[] = $warnings['expiredCred'];
      }
      $row['data']['info']['data'] = [
        '#theme' => 'item_list',
        '#items' => $items,
      ];
    }
    $rows[$warning_row_css_id] = $row;
  }

  /**
   * Renders the name of an app for the entity list.
   *
   * @param \Drupal\apigee_edge\Entity\AppInterface $app
   *   App entity.
   *
   * @return array
   *   Render array with the app name.
   */
  protected function renderAppName(AppInterface $app) : array {
    if ($app
      ->access('view')) {
      return $app
        ->toLink()
        ->toRenderable();
    }
    return [
      '#markup' => $app
        ->label(),
    ];
  }

  /**
   * Renders the status of an app for the entity list.
   *
   * @param \Drupal\apigee_edge\Entity\AppInterface $app
   *   App entity.
   *
   * @return array
   *   Render array with the app status.
   */
  protected function renderAppStatus(AppInterface $app) : array {
    $field = $this->entityTypeManager
      ->getViewBuilder($this->entityTypeId)
      ->viewField($app
      ->get('status'), 'default');
    $field['#label_display'] = 'hidden';
    return $field;
  }

  /**
   * Checks credentials of an app and returns warnings about them.
   *
   * @param \Drupal\apigee_edge\Entity\AppInterface $app
   *   The app entity to be checked.
   *
   * @return array
   *   An associative array that contains information about the revoked
   *   credentials and revoked or pending API products in a credential.
   */
  protected function checkAppCredentialWarnings(AppInterface $app) : array {
    return $this->appWarningsChecker
      ->getWarnings($app);
  }

  /**
   * Generates a unique CSS id for an app.
   *
   * @param \Drupal\apigee_edge\Entity\AppInterface $app
   *   App entity.
   *
   * @return string
   *   A unique CSS id for the app.
   */
  protected function generateCssIdForApp(AppInterface $app) : string {

    // App id (UUID) is unique by default.
    return Html::getId($app
      ->getAppId());
  }

  /**
   * Returns the CSS ID of the app info row.
   *
   * @param \Drupal\apigee_edge\Entity\AppInterface $app
   *   The app entity.
   *
   * @return string
   *   The CSS ID of the info row.
   */
  protected final function getCssIdForInfoRow(AppInterface $app) : string {
    return $this
      ->generateCssIdForApp($app) . '-info';
  }

  /**
   * Returns the CSS ID of the app warning row.
   *
   * @param \Drupal\apigee_edge\Entity\AppInterface $app
   *   The app entity.
   *
   * @return string
   *   The CSS ID of the warning row.
   */
  protected final function getCssIdForWarningRow(AppInterface $app) : string {
    return $this
      ->generateCssIdForApp($app) . '-warning';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AppListBuilder::$appWarningsChecker protected property The app warnings checker.
AppListBuilder::$configFactory protected property The config factory. Overrides EdgeEntityListBuilder::$configFactory
AppListBuilder::$renderer protected property The renderer service.
AppListBuilder::$requestStack protected property The request stack.
AppListBuilder::$time protected property The time service.
AppListBuilder::buildHeader public function Builds the header row for the entity listing. Overrides EntityListBuilder::buildHeader 1
AppListBuilder::buildInfoRow protected function Builds an info row for an app in the entity listing. 1
AppListBuilder::buildRow public function Builds a row for an entity in the entity listing. Overrides EntityListBuilder::buildRow
AppListBuilder::buildWarningRow protected function Builds a warning row for an app in the entity listing.
AppListBuilder::checkAppCredentialWarnings protected function Checks credentials of an app and returns warnings about them.
AppListBuilder::createInstance public static function Instantiates a new instance of this entity handler. Overrides EdgeEntityListBuilder::createInstance 2
AppListBuilder::generateCssIdForApp protected function Generates a unique CSS id for an app. 1
AppListBuilder::getCssIdForInfoRow final protected function Returns the CSS ID of the app info row.
AppListBuilder::getCssIdForWarningRow final protected function Returns the CSS ID of the app warning row.
AppListBuilder::getDefaultOperations protected function Gets this list's default operations. Overrides EntityListBuilder::getDefaultOperations 1
AppListBuilder::render public function Builds the entity listing as renderable array for table.html.twig. Overrides EdgeEntityListBuilder::render 1
AppListBuilder::renderAppName protected function Renders the name of an app for the entity list. 1
AppListBuilder::renderAppStatus protected function Renders the status of an app for the entity list.
AppListBuilder::__construct public function AppListBuilder constructor. Overrides EdgeEntityListBuilder::__construct 2
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EdgeEntityListBuilder::$entityTypeManager protected property The entity type manager service.
EdgeEntityListBuilder::buildEntityIdQuery protected function Builds an entity query used by entity listing. 2
EdgeEntityListBuilder::DEFAULT_DISPLAY_TYPE constant The default display type.
EdgeEntityListBuilder::getCacheMaxAge protected function Returns the cache max age. 2
EdgeEntityListBuilder::getDisplaySettings protected function Returns the display settings.
EdgeEntityListBuilder::getEntityIds final protected function Loads entity IDs using a pager sorted by the entity id. Overrides EntityListBuilder::getEntityIds
EdgeEntityListBuilder::renderUsingViewMode protected function Renders a list of entities using the provided view mode.
EdgeEntityListBuilder::usingDisplayType protected function Returns TRUE if entity type is configure to use provided display type.
EdgeEntityListBuilder::VIEW_MODE_DISPLAY_TYPE constant The view mode display type.
EntityHandlerBase::$moduleHandler protected property The module handler to invoke hooks on. 2
EntityHandlerBase::moduleHandler protected function Gets the module handler. 2
EntityHandlerBase::setModuleHandler public function Sets the module handler for this handler.
EntityListBuilder::$entityType protected property Information about the entity type.
EntityListBuilder::$entityTypeId protected property The entity type ID.
EntityListBuilder::$limit protected property The number of entities to list per page, or FALSE to list all entities. 3
EntityListBuilder::$storage protected property The entity storage class. 1
EntityListBuilder::buildOperations public function Builds a renderable list of operation links for the entity. 2
EntityListBuilder::ensureDestination protected function Ensures that a destination is present on the given URL.
EntityListBuilder::getLabel Deprecated protected function Gets the label of an entity.
EntityListBuilder::getOperations public function Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface::getOperations 2
EntityListBuilder::getStorage public function Gets the entity storage. Overrides EntityListBuilderInterface::getStorage
EntityListBuilder::getTitle protected function Gets the title of the page. 1
EntityListBuilder::load public function Loads entities of this type from storage for listing. Overrides EntityListBuilderInterface::load 4
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.