How to Enhance Schema.org Metatag Module in Drupal with Custom Fields

26 / Jun / 2024 by Prasad Deole 0 comments

Introduction

Schema.org is a system designed to enhance how search engines interpret your website’s content. The Schema.org Metatags module in Drupal simplifies the process of incorporating this information, thereby improving your site’s search engine rankings and increasing the likelihood of displaying rich, informative snippets in search results.

In this blog, we’ll explore how to expand the Schema.org Metatags module step-by-step. This will allow us to customize and use the module to meet specific needs that aren’t covered by its default settings.

For an introduction to using the Schema.org module, you can refer to our previous blog “Boost Your Drupal Site’s Visibility with Schema Markup“. This will provide a foundational understanding of how to implement Schema.org markup effectively.

Use Case

To start implementing, it’s important to understand why creating custom fields in the Drupal Schema.org Metatags module is necessary. The module comes with predefined schemas containing default fields and properties for specific types of schema. However, there are often cases where additional fields are needed that aren’t available in the module’s default schemas. In such situations, custom fields allow us to add the required metadata to existing schema groups, ensuring our content meets both Schema.org standards and specific project needs.

Implement Custom Fields in Drupal’s Schema.org Metatags

Let’s start creating and adding custom fields in Drupal’s Schema.org Metatags module.

Prerequisites

To proceed with the implementation, ensure the following prerequisites are met:

  1. Schema.org Metatags Module: Ensure the Schema.org Metatags module is installed and its version is above 3.0.0.
  2. Custom Module: Ensure you have an existing custom module to which you can add the meta tag. If a custom module does not exist, create one

Before starting the implementation, review the current snapshot of the organizational schema without any custom fields incorporated:

Schema without custom field

Organization Schema without custom field


Step 1: Define the Meta Tag Plugin

To define your custom meta tag, create a PHP class in your custom module under the directory custom_module/src/Plugin/metatag/Tag.

  • Navigate to /modules/custom/custom_module/src/Plugin/metatag/Tag.
  • Create a new file named CustomField.php. (Class name can be anything according to the requirements).
    Folder Structure

    Custom field folder structure

     

  • Define your meta tag with annotations as shown below:
    <?php
    
    namespace Drupal\custom_module\Plugin\metatag\Tag;
    
    use Drupal\schema_metatag\Plugin\metatag\Tag\SchemaNameBase;
    
    /**
    * Provides a plugin for the 'custom_field' meta tag.
    *
    * @MetatagTag(
    * id = "custom_field",
    * label = @Translation("Custom Field"),
    * description = @Translation("A custom field for demo purposes."),
    * name = "custom_field",
    * group = "schema_organization",
    * weight = 1,
    * type = "string",
    * secure = FALSE,
    * multiple = FALSE,
    * property_type = "text",
    * tree_parent = {},
    * tree_depth = -1,
    * )
    */
    class CustomField extends SchemaNameBase {
    }

    Note: Here we have used schema_organization as a group, any other group can be used according to the requirement.

Step 2: Declare the Field in Configuration

To make your new meta tag available, declare it in your module’s configuration.

  • Create a ‘config/’ directory in your custom module if it doesn’t already exist.
  • Add a configuration file for your meta tag. For example, create a file named custom_module.metatag_tag.schema.yml in the config/ directory:
    config file folder structure

    config file folder structure

     

  • Declare field with type and label for the same:
    # The 'type' should be "label" for short meta tags and "text" for ones which
    # could get longer, especially ones which use a textarea field instead of a
    # textfield.
    metatag.metatag_tag.custom_field:
    type: string
    label: 'Custom Field'
  • Clear all cache.

Step 3: Test your custom field

  • Navigate to the /admin/config/search/metatag.
  • Here we will be checking the Global metatag in the organization schema, as we have added the new custom field.

    Custom Field

    Custom Field Addition to the Organization type schema

  • After adding the custom field to the organization type schema, the final output should resemble the following snapshot:

    Custom Field Addition

    Custom Field Addition

Conclusion

Following these steps, you’ve successfully extended the schema_metatag module to include a custom meta tag within your existing Drupal module. This approach can be tailored for various types of metadata, enhancing your Drupal site’s SEO and metadata management. For more information on the schema metatag Drupal module, you may also refer to the official documentation of the module.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *