0

I have a custom drupal module which will generates pages for players in my site. The players stat data is meant to be updated most of the time, so for ease of use, I have created a custom module for the player pages.

I have an external db where all my players information are stored. My module will define a url pattern and once the pattern is obtained, it will display the players information from the external db.

My issue is, since there is no actual node present for my pages, I was not able to display meta tags and description for my player pages. I have tried meta tags module and meta tag routes modules. But by using these modules, I was only able to define a static meta tag and description for my entire module, means there is only one meta description for all pages. I want them to be dynamic and should vary from page to page. I have tried the examples provided below, but I can add only static string. I want the player wise meta description.

Generate meta in drupal 8 controller

Add meta tag to head in drupal 8

Here is my code structure.

player.info.yml

name: Player Module
description: Creates Page for Player.
package: Custom
type: module
version: 1.0
core: 8.x

player.module

<?php

/**
 * @file
 * Player module.
 */

/**
 * Implements hook_theme().
 */
function player_theme() {
  return [
    'player_page_data' => [
      'variables' => ['result' => NULL],
      'template' => 'player-page-formatter',
    ],
  ];
}

player.routing.yml

player:
  path: '/player/{player}'
  defaults:
    _controller: '\Drupal\player\Controller\PlayerController::content'
    _title_callback: '\Drupal\player\Controller\PlayerController::title' 
  requirements:
    _permission: 'access content'

PlayerController.php

<?php

namespace Drupal\player\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal;

class PlayerController extends ControllerBase {

  public function content($player) {
    $result = [];
    /* --------------------
     code logic here 
   -------------------- */
    return [
      '#theme' => 'player_page_data',
      '#result' => $result,
      '#cache' => ['max-age' => 0],
    ];

  }

  public function title($player) {
    $title = "";
    /* --------------
     code logic here 
    ------------------ */
    return [
      '#type' => 'markup',
      '#markup' => $title,
    ];
  }

}

I am stuck with this issue for long time. Any help will be appreciated.

Edit 1

Thank you for the support, now I am able to add meta tags. But I am using drupal meta tag module for my other pages. Here it shows the default meta tags as well as my meta tags. Some crawlers might ignore mine if they find multiple meta tags with same name. How do I disable my global meta tags and display only mine?

Browse other questions tagged or ask your own question.