Missing field 'hasCourseInstance'

Created on 21 December 2023, about 1 year ago

Problem/Motivation

In google indexing I have been getting error Missing field 'hasCourseInstance'
It seems like there is no place for this field in course schema

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

2.0

Component

Other tags

Created by

πŸ‡¦πŸ‡ΊAustralia samundrasharma

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

Comments & Activities

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

    This is an issue for the Schema.org Metatag module, rather than Metatag itself.

  • πŸ‡ΊπŸ‡ΈUnited States eojthebrave Minneapolis, MN

    I was able to work around this by adding a new hasCourseInstance field to the Course type and then populating in an implementation of hook_metatags_alter(). My solution adds the 'hasCourseInstance' field, and via the module UI you can add a 'CourseInstance' to the field. But I haven't yet been able to figure out how to add CourseInstance specific fields to the type. Right now it just uses those defined by the Event type.

    Do I need to create a new PropertyType of CourseInstance that duplicates the existing Event type and adds the custom fields? Or is there a better way to handle the CourseInstance specific fields like 'courseMode' and 'instructor'?

    Here's the plugin code I used for the hasCourseInstance field:

    
    namespace Drupal\dme\Plugin\metatag\Tag;
    
    use Drupal\schema_metatag\Plugin\metatag\Tag\SchemaNameBase;
    
    /**
     * Provides a plugin for the 'schema_course_hasCourseInstance' meta tag.
     *
     * Custom schema.org field for CourseInstance data. Can be removed when
     * https://www.drupal.org/project/schema_metatag/issues/3410151 is resolved.
     *
     * @MetatagTag(
     *   id = "schema_course_hasCourseInstance",
     *   label = @Translation("hasCourseInstance"),
     *   description = @Translation("RECOMMENDED BY GOOGLE. The course instance(s)."),
     *   name = "hasCourseInstance",
     *   group = "schema_course",
     *   weight = "0",
     *   type = "string",
     *   secure = FALSE,
     *   multiple = TRUE,
     *   property_type = "event",
     *   tree_parent = {
     *     "CourseInstance"
     *   },
     *   tree_depth = 0,
     * )
     */
    class SchemaCourseHasCourseInstance extends SchemaNameBase {
    
    }
    

    And the code I used to fill it in with the custom fields:

    /**
     * Implements hook_metatags_alter().
     */
    function dme_metatags_alter(array &$metatags, array &$context) {
          $courseInstance = [
            '@type' => 'CourseInstance',
            'courseMode' => 'online',
          ];
          $metatags['schema_course_hasCourseInstance'] = serialize($courseInstance);
      }
    }
    

    In case this is helpful for anyone else.

  • πŸ‡«πŸ‡·France Arono974

    Hello,

    Can you give me more details about the implementation of your solution?

    I've created a "hasCourseInstance" field on the content type and activated the module with your code but I get the error
    "Undefined array key "entity_type" in dme_metatags_alter() (line 7 of modules/dme/dme.module)."
    I'm using schema metatag version 3.0.3

    Thanks in advance and have a nice day,

  • πŸ‡¬πŸ‡§United Kingdom the_g_bomb

    I think this should be closed in favour of using ✨ Missing schema tags for courses Active which will be a part of the module and won't require custom code.

  • πŸ‡¬πŸ‡§United Kingdom the_g_bomb
Production build 0.71.5 2024