Missing field 'hasCourseInstance'

Created on 21 December 2023, 6 months ago
Updated 2 January 2024, 6 months 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

3.0

Component

Code

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.

Production build 0.69.0 2024