You are here

public function AmpMetadataListBuilder::render in Accelerated Mobile Pages (AMP) 8

Builds the entity listing as renderable array for table.html.twig.

@todo Add a link to add a new item to the #empty text.

Overrides EntityListBuilder::render

File

src/AmpMetadataListBuilder.php, line 252

Class

AmpMetadataListBuilder
Provides a listing of AMP Metadata entities.

Namespace

Drupal\amp

Code

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();
}