You are here

class AmpSettingsForm in Accelerated Mobile Pages (AMP) 8.3

Same name and namespace in other branches
  1. 8 src/Form/AmpSettingsForm.php \Drupal\amp\Form\AmpSettingsForm
  2. 8.2 src/Form/AmpSettingsForm.php \Drupal\amp\Form\AmpSettingsForm

Defines the configuration export form.

Hierarchy

Expanded class hierarchy of AmpSettingsForm

1 string reference to 'AmpSettingsForm'
amp.routing.yml in ./amp.routing.yml
amp.routing.yml

File

src/Form/AmpSettingsForm.php, line 17

Namespace

Drupal\amp\Form
View source
class AmpSettingsForm extends ConfigFormBase {

  /**
   * The theme handler service.
   *
   * @var \Drupal\Core\Extension\ThemeHandlerInterface
   */
  protected $themeHandler;

  /**
   * The array of valid theme options.
   *
   * @array $themeOptions
   */
  private $themeOptions;

  /**
   * The cache tags invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $tagInvalidate;

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

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'amp_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'amp.settings',
      'amp.theme',
    ];
  }

  /*
   * Helper function to get available theme options.
   *
   * @return array
   *   Array of valid themes.
   */
  private function getThemeOptions() {

    // Get all available themes.
    $themes = $this->themeHandler
      ->rebuildThemeData();
    uasort($themes, 'system_sort_modules_by_info_name');
    $theme_options = [];
    foreach ($themes as $theme) {
      if (!empty($theme->info['hidden'])) {
        continue;
      }
      elseif (!empty($theme->status)) {
        $theme_options[$theme
          ->getName()] = $theme->info['name'];
      }
    }
    return $theme_options;
  }

  /**
   * Constructs a AmpSettingsForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
   *   The theme handler.
   * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $tag_invalidate
   *   The cache tags invalidator.
   * @param \Drupal\amp\EntityTypeInfo $entity_type_info
   *   Information about AMP-enabled content types.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ThemeHandlerInterface $theme_handler, CacheTagsInvalidatorInterface $tag_invalidate, EntityTypeInfo $entity_type_info) {
    parent::__construct($config_factory);
    $this->themeHandler = $theme_handler;
    $this->themeOptions = $this
      ->getThemeOptions();
    $this->tagInvalidate = $tag_invalidate;
    $this->entityTypeInfo = $entity_type_info;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('theme_handler'), $container
      ->get('cache_tags.invalidator'), $container
      ->get('amp.entity_type'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $amp_config = $this
      ->config('amp.settings');
    $module_handler = \Drupal::moduleHandler();
    $page_prefix = $this
      ->t('<p>This page contains configuration for AMP ' . 'pages. Review <a href=":doclink1">Drupal Documentation</a> for the ' . '<a href=":doclink2">Drupal AMP module</a> and the <a href=":doclink3">' . 'AMP Project Page</a> for more information.</p>', [
      ':doclink1' => 'https://www.drupal.org/docs/8/modules/accelerated-mobile-pages-amp/amp-version-83',
      ':doclink2' => 'https://www.drupal.org/project/amp',
      ':doclink3' => 'https://www.ampproject.org',
    ]);
    $page_prefix .= '<ul>';
    if (!$module_handler
      ->moduleExists('schema_metatag')) {
      $page_prefix .= '<li>';
      $page_prefix .= $this
        ->t('Valid AMP requires Schema.org markup, which ' . 'can be provided by the <a href=":doclink2">Schema.org Metatag ' . 'module</a>.', [
        ':doclink2' => 'https://www.drupal.org/project/schema_metatag',
      ]);
      $page_prefix .= '</li>';
    }
    if ($module_handler
      ->moduleExists('toolbar') && !$module_handler
      ->moduleExists('amp_toolbar')) {
      $page_prefix .= '<li>';
      $page_prefix .= $this
        ->t('If you have the Toolbar module enabled, ' . 'enable the <a href=":doclink3">AMP Toolbar</a> module.', [
        ':doclink3' => '/admin/modules',
      ]);
      $page_prefix .= '</li>';
    }
    if ($module_handler
      ->moduleExists('rdf') && !$module_handler
      ->moduleExists('amp_rdf')) {
      $page_prefix .= '<li>';
      $page_prefix .= $this
        ->t('If you have the RDF module enabled, enable ' . 'the <a href=":doclink4">AMP RDF</a> module.', [
        ':doclink4' => '/admin/modules',
      ]);
      $page_prefix .= '</li>';
    }
    $page_prefix .= '</ul>';
    $amptheme_config = $this
      ->config('amp.theme');
    $description = $this
      ->t('Choose a theme to use for AMP pages. Themes must ' . 'be installed (but not necessarily set as the default theme) before ' . 'they will appear in this list and be usable by AMP. You can choose ' . 'between AMP Base, an installed subtheme of AMP Base, such as the ' . 'ExAMPle Subtheme, or any theme that complies with AMP rules. See ' . '<a href=":link">AMPTheme</a> for examples and pre-configured themes.', [
      ':link' => 'https://www.drupal.org/project/amptheme',
    ]);
    $form['theme'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Theme'),
      '#prefix' => $page_prefix,
      '#open' => TRUE,
    ];
    $form['theme']['amptheme'] = [
      '#type' => 'select',
      '#options' => $this->themeOptions,
      '#required' => TRUE,
      '#title' => $this
        ->t('AMP theme'),
      '#description' => $description,
      '#default_value' => $amptheme_config
        ->get('amptheme'),
    ];
    $prefix = $this
      ->t('<p>Select the content types you want to enable for ' . 'AMP in the list below. Enable them by turning on the AMP view mode ' . 'for that type. Once enabled, links are provided so you can configure ' . 'the fields and formatters for the AMP display of each one. For ' . 'instance, replace the normal text formatter for the body field with ' . 'the AMP text formatter, and replace the normal image formatter with ' . 'the AMP image formatter on the AMP view mode.</p>', [
      ':doclink1' => 'https://www.ampproject.org',
    ]);
    $form['types'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Content types'),
      '#open' => TRUE,
      '#description' => $prefix,
    ];
    if ($module_handler
      ->moduleExists('field_ui')) {
      $form['types']['amp_content_amp_status'] = [
        '#title' => $this
          ->t('AMP Status by Content Type'),
        '#theme' => 'table',
        '#header' => [
          t('Content type'),
          t('Enabled'),
          t('Configure'),
          t('Enable/Disable'),
        ],
        '#rows' => $this->entityTypeInfo
          ->getFormattedAmpEnabledTypes(),
      ];
    }
    else {
      $form['amp_content_amp_status'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('AMP Status by Content Type'),
        '#markup' => $this
          ->t('(In order to enable and disable AMP content ' . 'types in the UI, the Field UI module must be enabled.)'),
      ];
    }
    $page_suffix = $this
      ->t('This code uses the ' . '<a href="https://github.com/Lullabot/amp-library">AMP Library</a>. ' . 'This library will be installed by Composer if the AMP module is ' . 'installed by Composer as follows:</p><p><code>composer require ' . 'drupal/amp</code></p><p>Update the module using this:</p><p><code>' . 'composer update drupal/amp --with-dependencies</code></p>');
    $page_suffix .= $this
      ->t('Test that the AMP library is <a href=":url">' . 'configured properly</a>. Look for the words <strong>The Library is ' . 'working.</strong> at the top of the page. You will see that the ' . 'library detected markup that fails AMP standards. If the library is ' . 'not detected, retry adding the AMP module using Composer, as indicated ' . 'above.', [
      ':url' => Url::fromRoute('amp.test_library_hello')
        ->toString(),
    ]);
    $page_suffix .= '</p><p>';
    $page_suffix .= $this
      ->t('If you want to see AMP debugging information ' . 'for any node add "&debug#development=1" at end of the AMP node url, ' . 'e.g. <em>node/12345?amp&debug#development=1</em>. This will provide ' . 'Drupal messages on the page and AMP messages in the javascript ' . 'console. Check the AMP Project documentation for more information.</p>');
    $page_suffix .= '</p>';
    $form['library'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('AMP Library'),
      '#description' => $page_suffix,
      '#open' => TRUE,
    ];
    $form['advanced'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Advanced/Experimental Options'),
      '#open' => TRUE,
    ];
    $form['advanced']['process_full_html'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('<strong>Advanced option (Not recommended)' . '</strong>: Run the page body through the AMP library'),
      '#default_value' => $amp_config
        ->get('process_full_html'),
      '#description' => $this
        ->t('The AMP PHP library will fix some AMP HTML ' . 'non-compliance issues by removing disallowed attributes, tags ' . 'and property values. This is an option for fixing stubborn ' . 'AMP-unfriendly HTML. This feature can be problematic, the library ' . 'is often over-aggressive and removes some code you may still want, ' . 'so test carefully.'),
    ];
    $form['advanced']['amp_everywhere'] = [
      '#type' => 'checkbox',
      '#default_value' => $amp_config
        ->get('amp_everywhere'),
      '#title' => $this
        ->t('<strong>Experimental option</strong>: Generate all ' . 'pages as AMP pages'),
      '#description' => $this
        ->t('This is a new, experimental, option to ' . 'display your whole site as AMP pages. This assumes you understand ' . 'what is required to comply with AMP rules and are using an AMP-' . 'friendly theme as your primary theme, and using AMP formatters and ' . 'blocks in your primary theme. Leave unset if you want AMP pages ' . 'displayed as an alternative to your normal pages, on a different ' . 'path, the traditional way of deploying AMP. Check the box if your ' . 'normal pages <em>ARE</em> AMP pages, and serve as both the canonical ' . 'page and the AMP page. If you are not sure what what this means, ' . 'leave it unchecked.'),
    ];
    $form['show_extra_save_buttons'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Add extra save options to node edit pages'),
      '#default_value' => $amp_config
        ->get('show_extra_save_buttons'),
      '#description' => $this
        ->t('Adds convenient buttons for viewing the AMP version of a node after saving it (if the content type has AMP enabled).'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // AMP theme settings.
    $amptheme = $form_state
      ->getValue('amptheme');
    $amptheme_config = $this
      ->config('amp.theme');
    $amptheme_config
      ->setData([
      'amptheme' => $amptheme,
    ]);
    $amptheme_config
      ->save();
    $amp_config = $this
      ->config('amp.settings');
    $amp_config
      ->set('process_full_html', $form_state
      ->getValue('process_full_html'))
      ->save();
    $amp_config
      ->set('amp_everywhere', $form_state
      ->getValue('amp_everywhere'))
      ->save();
    $amp_config
      ->set('show_extra_save_buttons', $form_state
      ->getValue('show_extra_save_buttons'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AmpSettingsForm::$entityTypeInfo protected property Information about AMP-enabled content types.
AmpSettingsForm::$tagInvalidate protected property The cache tags invalidator.
AmpSettingsForm::$themeHandler protected property The theme handler service.
AmpSettingsForm::$themeOptions private property The array of valid theme options.
AmpSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
AmpSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
AmpSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
AmpSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AmpSettingsForm::getThemeOptions private function
AmpSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
AmpSettingsForm::__construct public function Constructs a AmpSettingsForm object. Overrides ConfigFormBase::__construct
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
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
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.