From d609c06e83e003554ed8766f399480c6ac1292d0 Mon Sep 17 00:00:00 2001
From: jrockowitz <3537824+jrockowitz@users.noreply.github.com>
Date: Sat, 8 Mar 2025 06:31:26 -0500
Subject: [PATCH] Issue #3511774: Define Schema.org types that should have a
 meta tags field

---
 modules/schemadotorg_metatag/README.md         |  1 +
 .../install/schemadotorg_metatag.settings.yml  |  2 ++
 .../schemadotorg_metatag.settings.schema.yml   |  6 ++++++
 .../schemadotorg_metatag.install               | 18 ++++++++++++++++++
 .../schemadotorg_metatag.module                | 10 ++++++++++
 .../src/SchemaDotOrgMetatagManager.php         |  7 +++++--
 6 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 modules/schemadotorg_metatag/schemadotorg_metatag.install

diff --git a/modules/schemadotorg_metatag/README.md b/modules/schemadotorg_metatag/README.md
index 9fb0ded7c..513a3622c 100644
--- a/modules/schemadotorg_metatag/README.md
+++ b/modules/schemadotorg_metatag/README.md
@@ -44,6 +44,7 @@ Configuration
 - Go to the Schema.org types configuration page.  
   (/admin/config/schemadotorg/settings/types#edit-schemadotorg-metatag)
 - Go to the 'Metatag settings' details.
+- Enter the Schema.org types that should have a meta tags field added when the Schema.org mapping is created.
 - Enter allowed meta tag groups to be displayed on node edit forms.
 
 - Go to the Schema.org properties configuration page.  
diff --git a/modules/schemadotorg_metatag/config/install/schemadotorg_metatag.settings.yml b/modules/schemadotorg_metatag/config/install/schemadotorg_metatag.settings.yml
index 33b5897da..8c77322d0 100644
--- a/modules/schemadotorg_metatag/config/install/schemadotorg_metatag.settings.yml
+++ b/modules/schemadotorg_metatag/config/install/schemadotorg_metatag.settings.yml
@@ -1,3 +1,5 @@
+schema_types:
+  - node--Thing
 default_groups:
   - basic
   - advanced
diff --git a/modules/schemadotorg_metatag/config/schema/schemadotorg_metatag.settings.schema.yml b/modules/schemadotorg_metatag/config/schema/schemadotorg_metatag.settings.schema.yml
index 26a0cc139..ff063c215 100644
--- a/modules/schemadotorg_metatag/config/schema/schemadotorg_metatag.settings.schema.yml
+++ b/modules/schemadotorg_metatag/config/schema/schemadotorg_metatag.settings.schema.yml
@@ -2,6 +2,12 @@ schemadotorg_metatag.settings:
   type: config_object
   label: 'Schema.org Metatag settings'
   mapping:
+    schema_types:
+      type: sequence
+      label: 'Schema.org types'
+      sequence:
+        type: string
+        label: 'Schema.org types'
     default_groups:
       type: sequence
       label: 'Default groups'
diff --git a/modules/schemadotorg_metatag/schemadotorg_metatag.install b/modules/schemadotorg_metatag/schemadotorg_metatag.install
new file mode 100644
index 000000000..ab452b75f
--- /dev/null
+++ b/modules/schemadotorg_metatag/schemadotorg_metatag.install
@@ -0,0 +1,18 @@
+<?php
+
+/**
+ * @file
+ * Installation hooks for the Schema.org Blueprints Metatag module.
+ */
+
+declare(strict_types=1);
+
+/**
+ * Issue #3511774: Define Schema.org types that should have a meta tags field.
+ */
+function schemadotorg_metatag_update_10000(): void {
+  \Drupal::configFactory()
+    ->getEditable('schemadotorg_metatag.settings')
+    ->set('schema_types', ['node--Thing'])
+    ->save();
+}
diff --git a/modules/schemadotorg_metatag/schemadotorg_metatag.module b/modules/schemadotorg_metatag/schemadotorg_metatag.module
index ccc2d0bc8..341626367 100644
--- a/modules/schemadotorg_metatag/schemadotorg_metatag.module
+++ b/modules/schemadotorg_metatag/schemadotorg_metatag.module
@@ -61,6 +61,16 @@ function schemadotorg_metatag_form_schemadotorg_types_settings_form_alter(array
     '#type' => 'details',
     '#title' => t('Metatag settings'),
   ];
+  $form['schemadotorg_metatag']['schema_types'] = [
+    '#type' => 'schemadotorg_settings',
+    '#title' => t('Schema.org type'),
+    '#description' => t('Enter the Schema.org types that should have a meta tags field added when the Schema.org mapping is created.'),
+    '#example' => '
+- entity_type_id--SchemaType
+- entity_type_id--bundle
+- entity_type_id--bundle--SchemaType
+',
+  ];
   $form['schemadotorg_metatag']['default_groups'] = [
     '#type' => 'checkboxes',
     '#title' => t('Default meta tag groups'),
diff --git a/modules/schemadotorg_metatag/src/SchemaDotOrgMetatagManager.php b/modules/schemadotorg_metatag/src/SchemaDotOrgMetatagManager.php
index 196696911..b9ec5783e 100644
--- a/modules/schemadotorg_metatag/src/SchemaDotOrgMetatagManager.php
+++ b/modules/schemadotorg_metatag/src/SchemaDotOrgMetatagManager.php
@@ -60,8 +60,11 @@ class SchemaDotOrgMetatagManager implements SchemaDotOrgMetatagManagerInterface
     $bundle = $mapping->getTargetBundle();
     $field_name = 'field_metatag';
 
-    // Only add the meta tags field to node types.
-    if ($entity_type_id !== 'node') {
+    // Determine is the Schema.org mapping should have meta tag field.
+    $schema_types = $this->configFactory
+      ->get('schemadotorg_metatag.settings')
+      ->get('schema_types');
+    if (!$this->schemaTypeManager->getSetting($schema_types, $mapping)) {
       return;
     }
 
-- 
GitLab