1

Drupal core 9.3.19, Metatag module 1.21.0

We have a taxonomy term named 'KamTags' which contains 'name' field only.

The customer wants to define a bunch of KamTags taxonomy terms and have content team assign these predefined KamTags taxonomy terms to Basic Page content type.

This can be easily done in Structure -> Content Types -> Basic Page -> Manage Fields -> Add field: and select Taxonomy Term as the field type. The taxonomy term would now appears in the Basic Page Edit page body:

enter image description here

Here's where it gets tricky:

Instead of Basic Edit page body, the customer wants the taxonomy terms to appear and be managed in the right column, under Meta Tag -> Basic Tags section (with Page Title, Description, Keywords, etc). I could potentially talk him into having the KamTag terms form section under Meta Tag accordion, but the main goal remains to have taxonomy terms assignment appear in the right column.

enter image description here

Is this even possible? Has anyone done anything like that?

0

2 Answers 2

1

Display Field in the right sidebar for content type:

You cam do it from backend by using "Field Group" module:

  1. Go to Structure, “Content types” & click on “Manage form display” of content type.

  2. Click on “Add Field Group” & select “Details Sidebar” from “Add a new group” drop-down. Add “Additional Info” into Label.

  3. In “Manage form display” section, you will see newly added "Additional Info" group in the Disabled section.

  4. Move “Additional Info” above Tags(Taxonomy field) and add Tags under “Additional Info” field group.

  5. click on Save

3
  • This is accurate and very close to the solution I'm looking for. However, the reason why KamTags need to be specifically associated with Meta Tags, is because they need to be globally assigned in Configure>Search>Metatag, rather than to every content type individually. I was looking at Drupal hooks to see which one would allow MetaTag module altering, but couldn't find any.
    – bdereta
    Commented Oct 8, 2023 at 18:23
  • I 'll suggest to follow add field here: admin/config/search/metatag/global by this way Kam Tags/Any custom field configured meta tags level instead of node Commented Oct 9, 2023 at 18:49
  • The below mentioned approach more suitable as per logic which is kind of leverage the meta tag module code that globally assigned custom field/Kam Tags in Configure>Search>Metatag, rather than bind to every content type individually Commented Oct 9, 2023 at 19:29
1

To display custom field under Meta - Tags section

Step 1: Add field's Meta Tags Plugin :

file Add: 'KamTags.php' will be add in this particular folder structure:
custom_module\src\Plugin\metatag\Tag;

<?php

namespace Drupal\custom_module\Plugin\metatag\Tag;

use Drupal\metatag\Plugin\metatag\Tag\MetaNameBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\taxonomy\Entity\Term;

/**
 * The standard page title.
 *
 * @MetatagTag(
 *   id = "kamtags",
 *   label = @Translation("Kam Tags"),
 *   description = @Translation("The text to display in the title bar of a visitor's web browser when they view this page."),
 *   name = "kamtags",
 *   group = "basic",
 *   weight = -6,
 *   type = "label",
 *   secure = FALSE,
 *   multiple = FALSE,
 *   trimmable = TRUE
 * )
 */
class KamTags extends MetaNameBase {
  // Nothing here yet. Just a placeholder class for a plugin.
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function form(array $element = []) {
    $selected_kamtags = $this->value()['kamtags']['0']['target_id'];
    $form['field_kamtags'] = [
      '#type' => 'entity_autocomplete',
      '#target_type' => 'taxonomy_term',
      '#title' => $this->t('Kam Tags'),
      '#default_value' => $selected_kamtags ? Term::load($selected_kamtags) : null,
      '#tags' => TRUE,
      '#selection_settings' => [
        'target_bundles' => ['tags'],
      ],
      '#weight' => '0',
    ];

    return $form;
  }
}

As entity autocomplete field render array instead of term name for custom(KamTags) meta tags: to fix it, used following hook: in .module file

function custom_module_metatags_alter(array &$metatags, array &$context) {
  if(isset($metatags['kamtags'])){
    $tid = $metatags['kamtags']['field_kamtags'][0]['target_id'];
    if(isset($tid) && !empty($tid)){
      $term = Term::load($tid);
      if(isset($term)){
        // Render Term label
        $metatags['kamtags'] = $term->getName();
      }
    }
  }
}

Note:: In this approach you don't have to add field at entity level as custom meta tags plugin field bind at meta tags level, that you will see on following URLs:

#1: Meta tags config page: https://your-site_url/admin/config/search/metatag/global

enter image description here

#2: At entity/node level: https://your-site_url/node/2/edit

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.