Commit 52d05ca8 authored by Janak Singh's avatar Janak Singh
Browse files

Issue #3000624: Provide support for all supported metatags

parent 5716698e
Loading
Loading
Loading
Loading
+26 −36
Original line number Diff line number Diff line
@@ -15,45 +15,35 @@
  */
function context_metadata_metatags_alter(array &$metatags, array $context) {

  /** @var \Drupal\context\ContextManager $context_manager */
  $context_manager = \Drupal::service('context.manager');

  foreach ($context_manager->getActiveReactions('context_metadata') as $reaction) {
    $context_metadata = array_filter($reaction->execute());

    if (!empty($context_metadata)) {
      foreach ($context_metadata as $key => $value) {

        switch ($key) {
          case 'metadata_title':
            $metatags['title'] = $value;
            break;

          case 'metadata_description':
            $metatags['description'] = $value;
            break;

          case 'metadata_keywords':
            $metatags['keywords'] = $value;
            break;

          case 'metadata_canonical_url':
            $metatags['canonical_url'] = $value;
            break;

          // TODO: Add this once we have other metadata working.
          case 'metadata_h1_title':
            $metatags['h1'] = $value;
            break;

          case 'metadata_robots':
            $metatags['robots'] = $value;
            break;
  /** @var \Drupal\context\ContextManager $contextManager */
  $contextManager = \Drupal::service('context.manager');

  // Start the loopty loop.
  foreach ($contextManager->getActiveReactions('context_metadata') as $reaction) {
    // Weed out the chaff.
    $contextMetadata = array_filter($reaction->execute());

    // Unset the ID.
    unset($contextMetadata['id']);

    // Start the loopty loop.
    if (!empty($contextMetadata)) {
      foreach ($contextMetadata as $key => $value) {
        // Deal with arrays such as robots.
        if (is_array($value)) {
          $values = array_filter($value);

          // Only add if there are any values.
          if (!empty($values)) {
            $originalValues = $metatags[$key];
            $metatags[$key] = array_merge($originalValues, $values);
          }
        }
        else {
          $metatags[$key] = $value;
        }

      }
    }

  }

}
+25 −67
Original line number Diff line number Diff line
@@ -19,55 +19,23 @@ class ContextMetadata extends ContextReactionPluginBase {
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['metadata_title'] = [
      '#title' => $this->t('Meta Title'),
      '#description' => $this->t('Title goes here'),
      '#type' => 'textfield',
      '#maxlength' => 256,
      '#default_value' => $this->getConfiguration()['metadata_title'],
    ];
    // TODO DI metatag.manager service.
    $metatagManager = \Drupal::service('metatag.manager');

    $form['metadata_description'] = [
      '#title' => $this->t('Meta Description'),
      '#description' => $this->t('Meta Description'),
      '#type' => 'textfield',
      '#maxlength' => 400,
      '#default_value' => $this->getConfiguration()['metadata_description'],
    ];
    // Get the sorted tags.
    $sortedTags = $metatagManager->sortedTags();

    $form['metadata_keywords'] = [
      '#title' => $this->t('Meta Keywords'),
      '#description' => $this->t('Meta Keywords'),
      '#type' => 'textfield',
      '#maxlength' => 400,
      '#default_value' => $this->getConfiguration()['metadata_keywords'],
    ];
    $values = [];

    $form['metadata_canonical_url'] = [
      '#title' => $this->t('Canonical URL'),
      '#description' => $this->t('Canonical URL'),
      '#type' => 'textfield',
      '#maxlength' => 400,
      '#default_value' => $this->getConfiguration()['metadata_canonical_url'],
    ];

    // TODO: Add this once we have other metadata working.
    /*$form['metadata_h1_title'] = array(
    '#title' => $this->t('H1 tag'),
    '#description' => $this->t('Overrides the H1 title'),
    '#type' => 'textfield',
    '#maxlength' => 400,
    '#default_value' => $this->getConfiguration()['metadata_h1_title'],
    );*/
    // Check previous values.
    foreach ($sortedTags as $tagId => $tagDefinition) {
      if (isset($this->getConfiguration()[$tagId])) {
        $values[$tagId] = $this->getConfiguration()[$tagId];
      }
    }

    // TODO: Add this once we have other metadata working.
    /*$form['metadata_robots'] = array(
    '#title' => $this->t('Robots'),
    '#description' => $this->t('Robots'),
    '#type' => 'textfield',
    '#maxlength' => 400,
    '#default_value' => $this->getConfiguration()['metadata_robots'],
    );*/
    // Get the base metatag form.
    $form = $metatagManager->form($values, []);

    return $form;
  }
@@ -76,35 +44,25 @@ class ContextMetadata extends ContextReactionPluginBase {
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->setConfiguration([
      'metadata_title' => $form_state->getValue('metadata_title'),
      'metadata_description' => $form_state->getValue('metadata_description'),
      'metadata_keywords' => $form_state->getValue('metadata_keywords'),
      'metadata_canonical_url' => $form_state->getValue('metadata_canonical_url'),
      // TODO: 'metadata_h1_title' => $form_state->getValue('metadata_h1_title'),
      // TODO: 'metadata_robots' => $form_state->getValue('metadata_robots'),.
    ]);
    // TODO DI metatag.manager service.
    $metatagManager = \Drupal::service('metatag.manager');
    $sortedTags = $metatagManager->sortedTags();
    $conf = [];

    foreach ($sortedTags as $tagId => $tagDefinition) {
      if ($form_state->hasValue([$tagDefinition['group'], $tagId])) {
        $conf[$tagId] = $form_state->getValue([$tagDefinition['group'], $tagId]);
      }
    }

  /**
   * {@inheritdoc}
   */
  public function summary() {
    return $this->getConfiguration()['context_metadata'];
    $this->setConfiguration($conf);
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'metadata_title' => '',
      'metadata_description' => '',
      'metadata_keywords' => '',
      'metadata_canonical_url' => '',
      // TODO: 'metadata_h1_title' => '',
      // TODO: 'metadata_robots' => '',.
    ];
  public function summary() {
    return $this->getConfiguration()['context_metadata'];
  }

  /**