Metatag Annotation for new submodule

Created on 7 December 2023, 11 months ago
Updated 14 December 2023, 11 months ago

How do I add my own implementation like other sub modules do?

I want to create a submodule to integrate Content Security Policy meta tags.

For this, I need to display a list of checkboxes, and for each checkbox, there is a list of radio buttons, and a text area.

I tried following the approach that other submodules do, but I could not see an example of my current approach. Any documentation to guide me on this?

What would be correct annotation to display a list of checkboxes, as well as the radio buttons that should be triggered per checkbox, and text area?

πŸ“Œ Task
Status

Active

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @sidney.desousa
  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    It sounds like uou'll need to extend MetaNameBase::form(), MetaNameBase::value() and MetaNameBase::setValue(), possibly some others too. If you want to have test coverage you'll also need to extend MetaNameBase::getTestFormXpath(), MetaNameBase::getTestFormData(), MetaNameBase::getTestOutputExistsXpath() and MetaNameBase::getTestOutputValuesXpath().

  • #2 after digging a bit into other implementations I see a light at the end of the tunnel.

    My class looks like this

    <?php
    
    namespace Drupal\metatag_csp\Plugin\metatag\Tag;
    
    use Drupal\Core\Form\FormStateInterface;
    use Drupal\metatag\Plugin\metatag\Tag\MetaNameBase;
    use Drupal\Core\StringTranslation\StringTranslationTrait;
    
    
    /**
     * Provides the Content Security Policy (CSP) meta tag.
     *
     * @MetatagTag(
     *   id = "csp",
     *   label = @Translation("Content Security Policy"),
     *   description = @Translation("Specifies the Content Security Policy directives for this page."),
     *   name = "csp",
     *   group = "advanced",
     *   weight = 10,
     *   type = "http-equiv",
     *   secure = TRUE,
     *   multiple = FALSE
     * )
     */
    class ContentSecurityPolicy extends MetaNameBase {
    
      use StringTranslationTrait;
    
      /**
       * {@inheritdoc}
       */
      public function form(array $element = []): array {
        $form = [];
        $form['csp_default_src'] = [
          '#type' => 'details',
          '#title' => $this->t('default-src Directive'),
          '#open' => TRUE,
        ];
    
        $form['csp_default_src']['default-src'] = [
          '#type' => 'radios',
          '#default_value' => "",
          '#options' => [
            'self' => $this->t('Self'),
            'none' => $this->t('None'),
            'any' => $this->t('Any'),
            'n/a' => $this->t('n/a')
          ],
        ];
    
        $form['csp_default_src']['additional_resources'] = [
          '#type' => 'textarea',
          '#title' => $this->t('Additional Resources'),
          '#default_value' => "",
          '#description' => $this->t('Specify additional resources for default-src.'),
        ];
    
        return $form;
      }
    }
    

    When I go to a page and save my changes, I notice that the values are not saving and I am struggling to understand how this config can be saved and retrieved if exists.

Production build 0.71.5 2024