diff --git a/amp.links.action.yml b/amp.links.action.yml index 01fd541..c398044 100644 --- a/amp.links.action.yml +++ b/amp.links.action.yml @@ -3,3 +3,5 @@ entity.amp_metadata.add_form: title: 'Add AMP Metadata' appears_on: - entity.amp_metadata.collection + cache_tags: + - amp_available_metadata diff --git a/amp.module b/amp.module index 2c5c3e8..56234e9 100644 --- a/amp.module +++ b/amp.module @@ -402,6 +402,34 @@ function amp_view_modes_submit(&$form, \Drupal\Core\Form\FormStateInterface $for } /** + * Implements hook_preprocess_block() for block templates. + */ +function amp_preprocess_block(&$variables) { + switch ($variables['base_plugin_id']) { + case 'local_actions_block': + if (isset($variables['content']['entity.amp_metadata.add_form']) && $variables['content']['entity.amp_metadata.add_form']) { + $has_global_metadata = \Drupal::service('amp.metadata')->ampMetadataHasGlobal(); + $all_types_have_metadata = empty(\Drupal::service('amp.metadata')->getAmpContentTypesWithoutMetadataSettings()); + if ($has_global_metadata && $all_types_have_metadata) { + $variables['content']['entity.amp_metadata.add_form'] = [ + '#markup' => '

All available global and content type overrides have been added. Please edit existing settings below.

', + 'addmetadata' => [ + '#theme_wrappers' => [ + 'container' => [ + '#attributes' => [ + 'class' => 'hidden' + ] + ] + ], + 'addlink' => $variables['content']['entity.amp_metadata.add_form'] + ] + ]; + } + } + } +} + +/** * Helper function to build render array containing JSON-formatted AMP metadata. * * @param NodeInterface $node diff --git a/config/install/image.style.amp_metadata_content_image_696.yml b/config/install/image.style.amp_metadata_content_image_696.yml new file mode 100644 index 0000000..1916a8e --- /dev/null +++ b/config/install/image.style.amp_metadata_content_image_696.yml @@ -0,0 +1,18 @@ +name: amp_metadata_content_image_min_696px_wide +label: 'AMP Metadata: Content image (696px wide)' +effects: + 0efa62e2-b31e-43f1-a1f6-a350ea092f13: + id: image_scale + data: + width: 696 + height: null + upscale: true + weight: 1 + uuid: 0efa62e2-b31e-43f1-a1f6-a350ea092f13 +langcode: en +dependencies: + module: + - amp + enforced: + module: + -amp diff --git a/config/install/image.style.amp_metadata_logo_600x60.yml b/config/install/image.style.amp_metadata_logo_600x60.yml new file mode 100644 index 0000000..287fbdc --- /dev/null +++ b/config/install/image.style.amp_metadata_logo_600x60.yml @@ -0,0 +1,18 @@ +name: amp_metadata_logo_600x60 +label: 'AMP Metadata: Logo (600x60)' +effects: + e3b08820-12f7-4cf1-9f6a-25e493a1345c: + id: image_scale + data: + width: 600 + height: 60 + upscale: false + weight: 1 + uuid: e3b08820-12f7-4cf1-9f6a-25e493a1345c +langcode: en +dependencies: + module: + - amp + enforced: + module: + -amp diff --git a/src/AmpMetadataListBuilder.php b/src/AmpMetadataListBuilder.php index 4872a7e..5f8a3ad 100644 --- a/src/AmpMetadataListBuilder.php +++ b/src/AmpMetadataListBuilder.php @@ -2,13 +2,18 @@ namespace Drupal\amp; +use Drupal\amp\AmpMetadataInfo; +use Drupal\amp\AmpMetadataInterface; +use Drupal\amp\EntityTypeInfo; use Drupal\Core\Config\Entity\ConfigEntityListBuilder; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Entity\EntityTypeInterface; use Drupal\file\Entity\File; use Drupal\file\FileInterface; -use Drupal\amp\AmpMetadataInterface; use Drupal\image\Entity\ImageStyle; use Drupal\image\ImageStyleInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a listing of AMP Metadata entities. @@ -16,6 +21,50 @@ use Drupal\image\ImageStyleInterface; class AmpMetadataListBuilder extends ConfigEntityListBuilder { /** + * The AMP metadata info service. + * + * @var \Drupal\amp\AmpMetadataInfo + */ + protected $ampMetadataInfo; + + /** + * Information about AMP-enabled content types. + * + * @var \Drupal\amp\EntityTypeInfo + */ + protected $entityTypeInfo; + + /** + * {@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') + ); + } + + /** + * 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 content types. + */ + public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, AmpMetadataInfo $amp_metadata_info, EntityTypeInfo $entity_type_info) { + parent::__construct($entity_type, $storage); + $this->ampMetadataInfo = $amp_metadata_info; + $this->entityTypeInfo = $entity_type_info; + } + + /** * {@inheritdoc} */ public function load() { @@ -63,7 +112,7 @@ class AmpMetadataListBuilder extends ConfigEntityListBuilder { if ($entity->id() != 'global_metadata') { $prefix = '
'; - $label = $this->t('Settings for @type overrides', ['@type' => $entity->label()]); + $label = $this->t('Settings for @type @overrides', ['@type' => $entity->label(), '@overrides' => $this->ampMetadataInfo->ampMetadataHasGlobal() ? 'overrides' : '']); } else { $label = $this->t('Settings for global metadata'); @@ -188,9 +237,70 @@ class AmpMetadataListBuilder extends ConfigEntityListBuilder { * {@inheritdoc} */ public function render() { + $view = ''; + $add = ''; + $has_global = $this->ampMetadataInfo->ampMetadataHasGlobal(); + $has_types_without_settings = !empty($this->ampMetadataInfo->getAmpContentTypesWithoutMetadataSettings()); + $has_no_types_with_settings = FALSE; + if (count($this->entityTypeInfo->getAmpEnabledTypes()) === count($this->ampMetadataInfo->getAmpContentTypesWithoutMetadataSettings())) { + $has_no_types_with_settings = TRUE; + } + + // Add appropriate lanague about viewing settings. + 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( - '#markup' => '

' . t("The first metadata setttings you create will apply globally. After that, you can add metadata overrides for a specific AMP-enabled content type. The add button will no longer appear after you have overriden all available content types. To view a summary of the metadata settings for each metadata type, click on the title.") . '

', + '#cache' => [ + 'max-age' => 0 + ] ); + if (!empty($view) && !empty($add)) { + $build['header']['#markup'] = '

' . $view . '

' . $add . '

'; + } + elseif (empty($view) && !empty($add)) { + $build['header']['#markup'] = '

' . $add . '

'; + } + elseif (!empty($view) && empty($add)) { + $build['header']['#markup'] = '

' . $view . '

'; + } + else { + $build['header']['#markup'] = '

There should be a message here, but apparently something unforseen has happened.

'; + } + return $build + parent::render(); } } diff --git a/src/Form/AmpMetadataDeleteForm.php b/src/Form/AmpMetadataDeleteForm.php index 779793f..5b93e6b 100644 --- a/src/Form/AmpMetadataDeleteForm.php +++ b/src/Form/AmpMetadataDeleteForm.php @@ -2,14 +2,60 @@ namespace Drupal\amp\Form; +use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Drupal\Core\Entity\EntityConfirmFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; +use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Builds the form to delete AMP Metadata entities. */ -class AmpMetadataDeleteForm extends EntityConfirmFormBase { +class AmpMetadataDeleteForm extends EntityConfirmFormBase implements ContainerAwareInterface { + + /** + * The container. + * + * @var \Symfony\Component\DependencyInjection\ContainerInterface + */ + protected $container; + + /** + * The cache tags invalidator. + * + * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface + */ + protected $tagInvalidate; + + /** + * Constructs a AmpMetadataDeleteForm object. + * + * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $tag_invalidate + * The cache tags invalidator. + */ + public function __construct(CacheTagsInvalidatorInterface $tag_invalidate) { + $this->tagInvalidate = $tag_invalidate; + } + + /** + * Sets the Container. + * + * @param ContainerInterface|null $container A ContainerInterface instance or null + */ + public function setContainer(ContainerInterface $container = null) { + $this->container = $container; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('cache_tags.invalidator') + ); + } + /** * {@inheritdoc} */ @@ -36,6 +82,7 @@ class AmpMetadataDeleteForm extends EntityConfirmFormBase { */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->entity->delete(); + $this->tagInvalidate->invalidateTags(['amp_available_metadata']); drupal_set_message( $this->t('content @type: deleted @label.', diff --git a/src/Form/AmpMetadataForm.php b/src/Form/AmpMetadataForm.php index aa56989..3543391 100644 --- a/src/Form/AmpMetadataForm.php +++ b/src/Form/AmpMetadataForm.php @@ -4,6 +4,7 @@ namespace Drupal\amp\Form; use Drupal\amp\AmpMetadataInfo; use Drupal\amp\Entity\AmpMetadata; +use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Drupal\Core\Entity\EntityForm; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Form\FormStateInterface; @@ -14,7 +15,6 @@ use Drupal\file\FileUsage\DatabaseFileUsageBackend; use Drupal\image\ImageStyleInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\RedirectResponse; /** * Class AmpMetadataForm. @@ -45,14 +45,26 @@ class AmpMetadataForm extends EntityForm implements ContainerAwareInterface { protected $fileUsage; /** + * The cache tags invalidator. + * + * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface + */ + protected $tagInvalidate; + + /** * Constructs a AmpMetadataForm object. * * @param \Drupal\amp\AmpMetadataInfo $amp_metadata_info * The AMP metadata info service. + * @param \Drupal\file\FileUsage\DatabaseFileUsageBackend + * The file usage service. + * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $tag_invalidate + * The cache tags invalidator. */ - public function __construct(AmpMetadataInfo $amp_metadata_info, DatabaseFileUsageBackend $file_usage) { + public function __construct(AmpMetadataInfo $amp_metadata_info, DatabaseFileUsageBackend $file_usage, CacheTagsInvalidatorInterface $tag_invalidate) { $this->ampMetadataInfo = $amp_metadata_info; $this->fileUsage = $file_usage; + $this->tagInvalidate = $tag_invalidate; } /** @@ -70,7 +82,8 @@ class AmpMetadataForm extends EntityForm implements ContainerAwareInterface { public static function create(ContainerInterface $container) { return new static( $container->get('amp.metadata'), - $container->get('file.usage') + $container->get('file.usage'), + $container->get('cache_tags.invalidator') ); } @@ -110,7 +123,7 @@ class AmpMetadataForm extends EntityForm implements ContainerAwareInterface { $form['content_type'] = array( '#type' => 'select', '#title' => t('Content type'), - '#description' => t('Select a content type for which you would like to override the global AMP metadata setting.'), + '#description' => t('Select a content type for which you would like to override the global AMP metadata setting. Settings are overridden on a field by field basis.'), '#options' => $content_type_options, '#required' => TRUE, '#default_value' => NULL, @@ -182,7 +195,7 @@ class AmpMetadataForm extends EntityForm implements ContainerAwareInterface { '#type' => 'select', '#title' => t('Organization logo image style'), '#options' => image_style_options(TRUE), - '#description' => t('The image style to use for the organization logo'), + '#description' => t('The image style to use for the organization logo.'), '#default_value' => $org_logo_style? $org_logo_style : '', ]; @@ -298,6 +311,7 @@ class AmpMetadataForm extends EntityForm implements ContainerAwareInterface { '@type' => $this->entityTypeManager->getStorage('node_type')->load($content_type)->label() ])); } + $this->tagInvalidate->invalidateTags(['amp_available_metadata']); } // Save organization name.