You are here

class AmpMetadataListBuilder in Accelerated Mobile Pages (AMP) 8

Provides a listing of AMP Metadata entities.

Hierarchy

Expanded class hierarchy of AmpMetadataListBuilder

File

src/AmpMetadataListBuilder.php, line 23

Namespace

Drupal\amp
View source
class AmpMetadataListBuilder extends ConfigEntityListBuilder {

  /**
   * The AMP metadata info service.
   *
   * @var \Drupal\amp\AmpMetadataInfo
   */
  protected $ampMetadataInfo;

  /**
   * Information about AMP-enabled node types.
   *
   * @var \Drupal\amp\EntityTypeInfo
   */
  protected $entityTypeInfo;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('entity.manager')
      ->getStorage($entity_type
      ->id()), $container
      ->get('amp.metadata'), $container
      ->get('amp.entity_type'), $container
      ->get('module_handler'));
  }

  /**
   * Constructs a new EntityListBuilder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
   * @param \Drupal\amp\AmpMetadataInfo $amp_metadata_info
   *   The AMP metadata info service.
   * @param \Drupal\amp\EntityTypeInfo $entity_type_info
   *   Information about AMP-enabled node types.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, AmpMetadataInfo $amp_metadata_info, EntityTypeInfo $entity_type_info, ModuleHandlerInterface $module_handler) {
    parent::__construct($entity_type, $storage);
    $this->ampMetadataInfo = $amp_metadata_info;
    $this->entityTypeInfo = $entity_type_info;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public function load() {
    $entities = parent::load();

    // Move the Global defaults to the top if they exist.
    if (isset($entities['global'])) {
      return array(
        'global' => $entities['global'],
      ) + $entities;
    }
    else {
      return $entities;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['label'] = $this
      ->t('AMP Metadata type');
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    $row['label'] = $this
      ->getLabelAndConfig($entity);
    return $row + parent::buildRow($entity);
  }

  /**
   * Renders the metada settings label plus its configuration.
   *
   * @param AmpMetadataInterface $entity
   *   The metadata settings entity.
   *
   * @return
   *   Render array for a table cell.
   */
  public function getLabelAndConfig(EntityInterface $entity) {
    $prefix = '';
    $label = '';
    $has_settings = FALSE;
    if ($entity
      ->id() != 'global') {
      $prefix = '<div class="indentation"></div>';
      $label = $this
        ->t('Settings for @type @overrides', [
        '@type' => $entity
          ->label(),
        '@overrides' => $this->ampMetadataInfo
          ->ampMetadataHasGlobal() ? 'overrides' : '',
      ]);
    }
    else {
      $label = $this
        ->t('Settings for global metadata');
    }
    $details = [
      '#type' => 'table',
      '#title' => $label,
      '#header' => [
        'Setting',
        'Data',
      ],
    ];
    if (!empty($org_name = $entity
      ->getOrganizationName())) {
      $details['organization_name']['label'] = [
        '#markup' => '<strong>Organization name</strong>:',
      ];
      $details['organization_name']['value'] = [
        '#markup' => $org_name,
      ];
      $has_settings = TRUE;
    }

    /** @var FileInterface $org_logo_file */
    if (!empty($org_logo_fid = $entity
      ->getOrganizationLogoFid()) && !empty($org_logo_file = File::load($org_logo_fid))) {
      $details['organization_logo']['label'] = [
        '#markup' => '<strong>Organization logo file</strong>:',
      ];
      $details['organization_logo']['value'] = [
        '#markup' => $org_logo_file
          ->getFilename(),
      ];
      $has_settings = TRUE;
    }

    /** @var ImageStyleInterface $org_logo_image_style */
    if (!empty($org_logo_image_style_id = $entity
      ->getOrganizationLogoImageStyleId()) && !empty($org_logo_image_style = ImageStyle::load($org_logo_image_style_id))) {
      $details['organization_logo_image_style']['label'] = [
        '#markup' => '<strong>Organization logo image style</strong>:',
      ];
      $details['organization_logo_image_style']['value'] = [
        '#markup' => $org_logo_image_style
          ->getName(),
      ];
      $has_settings = TRUE;
    }
    if (!empty($content_headline = $entity
      ->getContentHeadlineToken())) {
      $details['content_headline']['label'] = [
        '#markup' => '<strong>Content headline token</strong>:',
      ];
      $details['content_headline']['value'] = [
        '#markup' => $content_headline,
      ];
      $has_settings = TRUE;
    }
    if (!empty($content_schema = $entity
      ->getContentSchemaType())) {
      $details['content_schema_type']['label'] = [
        '#markup' => '<strong>Content schema type</strong>:',
      ];
      $details['content_schema_type']['value'] = [
        '#markup' => $content_schema,
      ];
      $has_settings = TRUE;
    }
    if (!empty($content_author = $entity
      ->getContentAuthorToken())) {
      $details['content_author']['label'] = [
        '#markup' => '<strong>Content author token</strong>:',
      ];
      $details['content_author']['value'] = [
        '#markup' => $content_author,
      ];
      $has_settings = TRUE;
    }
    if (!empty($content_description = $entity
      ->getContentDescriptionToken())) {
      $details['content_description']['label'] = [
        '#markup' => '<strong>Content author token</strong>:',
      ];
      $details['content_description']['value'] = [
        '#markup' => $content_description,
      ];
      $has_settings = TRUE;
    }
    if (!empty($content_image = $entity
      ->getContentImageToken())) {
      $details['content_image']['label'] = [
        '#markup' => '<strong>Content image token</strong>:',
      ];
      $details['content_image']['value'] = [
        '#markup' => $content_image,
      ];
      $has_settings = TRUE;
    }

    /** @var ImageStyleInterface $content_image_style */
    if (!empty($content_image_style_id = $entity
      ->getContentImageStyleId()) && !empty($content_image_style = ImageStyle::load($content_image_style_id))) {
      $details['content_image_style']['label'] = [
        '#markup' => '<strong>Content image style</strong>:',
      ];
      $details['content_image_style']['value'] = [
        '#markup' => $content_image_style
          ->getName(),
      ];
      $has_settings = TRUE;
    }
    if (!$has_settings) {
      $details = [
        '#markup' => $this
          ->t('No settings for this metadata type.'),
      ];
    }
    return [
      'data' => [
        '#type' => 'details',
        '#prefix' => $prefix,
        '#title' => $entity
          ->label(),
        'details' => $details,
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $view = '';
    $add = '';
    $has_global = $this->ampMetadataInfo
      ->ampMetadataHasGlobal();
    $has_types_without_settings = !empty($this->ampMetadataInfo
      ->getAmpNodeTypesWithoutMetadataSettings());
    $has_no_types_with_settings = FALSE;
    if (count($this->entityTypeInfo
      ->getAmpEnabledTypes()) === count($this->ampMetadataInfo
      ->getAmpNodeTypesWithoutMetadataSettings())) {
      $has_no_types_with_settings = TRUE;
    }
    $has_no_amp_types = empty($this->entityTypeInfo
      ->getAmpEnabledTypes());

    // Add appropriate lanague about viewing settings.
    if ($has_no_amp_types) {
      $view = $this
        ->t('Learn how to enable AMP for content types on the <a href=":settings">AMP settings page</a>.', [
        ':settings' => Url::fromRoute('amp.settings')
          ->toString(),
      ]);
    }
    else {
      if (!$has_global && $has_no_types_with_settings) {
        $view = $this
          ->t('No metadata settings created yet.');
      }
      elseif ($has_global && $has_no_types_with_settings) {
        $view = $this
          ->t('To view a summary of the global metadata settings, click on the title.');
      }
      elseif (!$has_types_without_settings || $has_types_without_settings && !$has_no_types_with_settings) {
        if ($has_global) {
          $view = $this
            ->t('To view a summary of the settings for global metadata or for a content type override, click on the title of the settings you would like to view.');
        }
        else {
          $view = $this
            ->t('To view a summary of the settings for a content type, click on the title of the settings you would like to view.');
        }
      }

      // Add appropriate lanague about adding settings.
      if ($has_global) {
        if ($has_no_types_with_settings) {
          $add = $this
            ->t('You can also add settings overrides for AMP-enabled content types.');
        }
        elseif ($has_types_without_settings) {
          $add = $this
            ->t('You can also add settings for AMP-enabled content types that do not yet have overrides.');
        }
      }
      else {
        if ($has_no_types_with_settings) {
          $add = $this
            ->t('The first metadata setttings you create will apply globally.');
        }
        elseif ($has_types_without_settings) {
          $add = $this
            ->t('You can also add global AMP metadata settings or settings for AMP-enabled content types that do not yet have settings.');
        }
        else {
          $add = $this
            ->t('You can also add global AMP metadata settings.');
        }
      }
    }
    $build['header'] = array(
      '#cache' => [
        'max-age' => 0,
        'tags' => [
          'amp_types',
          'amp_available_metadata',
        ],
      ],
    );
    if (!empty($view) && !empty($add)) {
      $build['header']['#markup'] = '<p>' . $view . '</p><p>' . $add . '</p>';
    }
    elseif (empty($view) && !empty($add)) {
      $build['header']['#markup'] = '<p>' . $add . '</p>';
    }
    elseif (!empty($view) && empty($add)) {
      $build['header']['#markup'] = '<p>' . $view . '</p>';
    }
    else {
      $build['header']['#markup'] = '<p>There should be a message here, but apparently something unforseen has happened.</p>';
    }
    if (!$this->moduleHandler
      ->moduleExists('token')) {

      // Provide message in case somebody has upgraded AMP module but has not
      // installed Token.
      drupal_set_message($this
        ->t('The AMP module now requires the <a href="@module">Token</a> module as a dependency. Please download and install Token in order for AMP metadata to appear properly.', [
        '@module' => 'https://www.drupal.org/project/token',
      ]), 'warning');
    }
    return $build + parent::render();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AmpMetadataListBuilder::$ampMetadataInfo protected property The AMP metadata info service.
AmpMetadataListBuilder::$entityTypeInfo protected property Information about AMP-enabled node types.
AmpMetadataListBuilder::$moduleHandler protected property The module handler. Overrides EntityHandlerBase::$moduleHandler
AmpMetadataListBuilder::buildHeader public function Builds the header row for the entity listing. Overrides EntityListBuilder::buildHeader
AmpMetadataListBuilder::buildRow public function Builds a row for an entity in the entity listing. Overrides EntityListBuilder::buildRow
AmpMetadataListBuilder::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityListBuilder::createInstance
AmpMetadataListBuilder::getLabelAndConfig public function Renders the metada settings label plus its configuration.
AmpMetadataListBuilder::load public function Loads entities of this type from storage for listing. Overrides ConfigEntityListBuilder::load
AmpMetadataListBuilder::render public function Builds the entity listing as renderable array for table.html.twig. Overrides EntityListBuilder::render
AmpMetadataListBuilder::__construct public function Constructs a new EntityListBuilder object. Overrides EntityListBuilder::__construct
ConfigEntityListBuilder::getDefaultOperations public function Gets this list's default operations. Overrides EntityListBuilder::getDefaultOperations 15
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
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::getEntityIds protected function Loads entity IDs using a pager sorted by the entity id. 4
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
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.