Path Plugin

Path plugins is attached for specific pages applies on URL paths.

It's used when you need to add JSON-LD on pages with specific paths.

Annotation example

/**
 * @JsldPath(
 *   id = "organization",
 *   match_type = "listed",
 *   match_path = {"/about", "<front>", "/info", "/info/*"}
 * )
 */

This plugins is stored in /src/Plugin/jsld/path/ path.

Generate plugin

drush generate plugin-jsld-path

Example of plugin

<?php

namespace Drupal\MODULENAME\Plugin\jsld\path;

use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\jsld\Plugin\jsld\JsldPathPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * @JsldPath(
 *   id = "organization",
 *   match_type = "listed",
 *   match_path = {"*"}
 * )
 */
class Organization extends JsldPathPluginBase implements ContainerFactoryPluginInterface {

   /**
   * The current request.
   */
  protected Request $request;

  /**
   * The config system site.
   */
  protected ImmutableConfig $configSystemSite;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container,array $configuration,$plugin_id,$plugin_definition){
    $instance = new static($configuration, $plugin_id, $plugin_definition);
    $instance->request = $container->get('request_stack')->getCurrentRequest();
    $instance->configSystemSite = $container->get('config.factory')->get('system.site');

    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $host = $this->request->getSchemeAndHttpHost();
    return [
      '@context' => 'https://schema.org',
      '@type' => 'Organization',
      'name' => $this->configSystemSite->get('name'),
      'sameAs' => $host,
      'url' => $host,
    ];
  }

}