Unable to create new field with sub properties

Created on 12 May 2025, 3 months ago

I'm struggling to add a new field to the Product schema. I created a new submodule named 'Schema_Vehicle' where I was successfully able to add multiple new single fields such as VIN number, color, etc. However the 'milesFromOdometer' schema has sub properties, and no matter what I do (I have spent countless days on this now), I cannot get the fields to even display on the default node section nor the node itself.

This is an example of how the output should look:
"mileageFromOdometer": {
"@type": "QuantitativeValue",
"value": "20170",
"unitCode": "SMI"
},

Below is the code. I know all the namespaces and such are correct as the 8 other single fields I added, each with a file like this, work as expected. Not sure what ia m missing.

<?php

namespace Drupal\schema_vehicle\Plugin\schema_metatag\PropertyType;

use Drupal\schema_metatag\Plugin\schema_metatag\PropertyTypeBase;

/**
 * Provides a plugin for the 'schema_product_mileage' Schema.org property.
 *
 * @SchemaPropertyType(
 *   id = "schema_product_mileage",
 *   label = @Translation("Mileage From Odometer"),
 *   tree_parent = {
 *     "QuantitativeValue",
 *   },
 *   tree_depth = 2,
 *   property_type = "QuantitativeValue",
 *   sub_properties = {
 *     "@type" = {
 *       "id" = "type",
 *       "label" = @Translation("@type"),
 *       "description" = @Translation("The Schema.org type of this sub-object."),
 *     },
 *     "value" = {
 *       "id" = "number",
 *       "label" = @Translation("value"),
 *       "description" = @Translation("The numeric measurement value."),
 *     },
 *     "unitCode" = {
 *       "id" = "text",
 *       "label" = @Translation("unitCode"),
 *       "description" = @Translation("The unit of measurement, e.g. β€œSMI” for statute miles."),
 *     },
 *   },
 * )
 */
class SchemaProductMileage extends PropertyTypeBase {}
πŸ’¬ Support request
Status

Active

Version

3.0

Component

New structures

Created by

πŸ‡ΊπŸ‡ΈUnited States tyler-durden

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

Comments & Activities

  • Issue created by @tyler-durden
  • πŸ‡ΊπŸ‡ΈUnited States tyler-durden
  • πŸ‡³πŸ‡±Netherlands borisr

    Can you show a bit more of your code? That would make it easier to debug.
    Also, note there is a difference between a MetatagTag plugin and a SchemaPropertyType (you'll likely need both un your case).

  • πŸ‡ΊπŸ‡ΈUnited States tyler-durden

    I'm not understanding the question, which may be part of my issue. The complete code of the file is above for the file:
    schema_vehicle ->src ->Plugin ->metatag ->Tag ->SchemaProductMiles.php

    The code below works fine as a single field, so in trying to understand your message it looks like I need to somehow have a new SchemaPropertyType documented somewhere? I'm trying to find documentation for that now, if you have any insights on where to find this?

    <?php
    
    namespace Drupal\schema_vehicle\Plugin\metatag\Tag;
    
    use Drupal\schema_metatag\Plugin\metatag\Tag\SchemaNameBase;
    
    /**
     * Provides a plugin for the 'schema_product_VIN' meta tag.
     *
     * - 'id' should be a globally unique id.
     * - 'name' should match the Schema.org element name.
     * - 'group' should match the id of the group that defines the Schema.org type.
     *
     * @MetatagTag(
     *   id = "schema_product_VIN",
     *   label = @Translation("VIN Number"),
     *   description = @Translation("Vehicle ID number."),
     *   name = "VehicleIdentificationNumber",
     *   group = "schema_product",
     *   weight = 10,
     *   type = "string",
     *   secure = FALSE,
     *   multiple = FALSE,
     *   property_type = "text",
     *   tree_parent = {},
     *   tree_depth = -1,
     * )
     */
    class SchemaProductVIN extends SchemaNameBase {
    
    }
  • πŸ‡ΊπŸ‡ΈUnited States tyler-durden

    ok I found the 8.2 documentation and this subject is only touched on. Looks like that is stored in "/src/Plugin/schema_metatag/PropertyType".

    Would this be the proper place to create a new file for that? I want this safe from updates and not being overwritten when updated.

  • πŸ‡³πŸ‡±Netherlands borisr

    Hi Tyler,

    Thank you for providing some more context. I played around for a bit and it seems you will need both a SchemaPropertyType (something like your first provided example) and a MetatagTag plugin (something like your second example).

    The MetatagTag plugin then will need to use the new SchemaPropertyType as 'property_type' property. The SchemaPropertyType plugin can be placed in the 'src/Plugin/schema_metatag/PropertyType' directory of your custom module.

    Also see these plugins for reference:
    - https://git.drupalcode.org/project/schema_metatag/-/blob/3.0.3/src/Plugi...
    - https://git.drupalcode.org/project/schema_metatag/-/blob/3.0.3/schema_jo...

    I hope this will point you in the right direction but feel free to reach out if you need more help (or if my assumptions where incorrect).

  • πŸ‡³πŸ‡±Netherlands borisr

    Some example code might be easier to follow.

    File: schema_vehicle/src/Plugin/metatag/Tag/SchemaProductMileage.php

    Code:

    <?php
    
    namespace Drupal\schema_vehicle\Plugin\metatag\Tag;
    
    use Drupal\schema_metatag\Plugin\metatag\Tag\SchemaNameBase;
    
    /**
     * Provides a plugin for the 'mileageFromOdometer' meta tag.
     *
     * - 'id' should be a globally unique id.
     * - 'name' should match the Schema.org element name.
     * - 'group' should match the id of the group that defines the Schema.org type.
     *
     * @MetatagTag(
     *   id = "schema_product_mileage",
     *   label = @Translation("Mileage From Odometer"),
     *   name = "mileageFromOdometer",
     *   group = "schema_product",
     *   weight = 10,
     *   type = "string",
     *   secure = FALSE,
     *   multiple = FALSE,
     *   property_type = "product_vehicle_mileage",
     *   tree_parent = {
     *     "QuantitativeValue",
     *   },
     *   tree_depth = -1,
     * )
     */
    class SchemaProductMileage extends SchemaNameBase {
    
    }
    

    File: schema_vehicle/src/Plugin/schema_metatag/PropertyType/ProductVehicleMileage.php

    Code:

    <?php
    
    namespace Drupal\schema_vehicle\Plugin\schema_metatag\PropertyType;
    
    use Drupal\schema_metatag\Plugin\schema_metatag\PropertyTypeBase;
    
    /**
     * Provides a plugin for the 'mileageFromOdometer' Schema.org property.
     *
     * @SchemaPropertyType(
     *   id = "product_vehicle_mileage",
     *   label = @Translation("Mileage From Odometer"),
     *   tree_parent = {
     *     "QuantitativeValue",
     *   },
     *   tree_depth = 0,
     *   property_type = "QuantitativeValue",
     *   sub_properties = {
     *     "@type" = {
     *       "id" = "type",
     *       "label" = @Translation("@type"),
     *       "description" = @Translation("The Schema.org type of this sub-object."),
     *     },
     *     "value" = {
     *       "id" = "number",
     *       "label" = @Translation("value"),
     *       "description" = @Translation("The numeric measurement value."),
     *     },
     *     "unitCode" = {
     *       "id" = "text",
     *       "label" = @Translation("unitCode"),
     *       "description" = @Translation("The unit of measurement, e.g. β€œSMI” for statute miles."),
     *     },
     *   },
     * )
     */
    class ProductVehicleMileage extends PropertyTypeBase {
    
    }
    

    Note: the code above is just an example, to further clarify previous comments. If you plan to contribute the 'schema_vehicle' module, the property type should probably be placed in the main 'schema_metatag' module.

  • πŸ‡ΊπŸ‡ΈUnited States tyler-durden

    Thank you for all the explanations, I think I understand it now however I need to put this project on the back burner for a few months as I found bigger issues that need to be addressed with my site first. I'm closing this ticket so it does not clog up the system, and will reopen if I move forward with this and share with the community for vehicles if so.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024