1

I have a custom content entity type, mymodule, which currently generates paths like /mymodule/{entity_ID}/ on creation.

My entity has a title field defined programmatically in my baseFieldDefinitions() function.

Using the pathauto module, I am able to go to admin/config/search/path/settings and enable pathauto for my custom content entity type.

However, when I go to set up Patterns, the title field is not available as a token for this entity type. Meanwhile, I have other fields in this entity defined via the Drupal interface and not via baseFieldDefinitions(), all of which are available as tokens (unfortunately, none would be useful for a path alias).

Attempting "Bulk generate" for this entity type does not do anything. The progress bar shows that it's checking the instances of my entity type, says "Completed 2 of 2", but upon completion shows a status message saying "No new URL aliases to generate.", and no such paths appear in /admin/config/search/path.

The plugin works for me for core entity types like taxonomy term.

Update: Well, this is embarrassing. I had a file mymodule.tokens.inc that I completely forgot about. The 'title' token was not defined in hook_tokens() or hook_token_info(). Here is the full code of mymodule.tokens.inc (I believe this code can also be used in mymodule.module):

/**
 * @file
 * Builds placeholder replacement tokens for custom module data.
 */

use Drupal\Core\Render\BubbleableMetadata;

/**
 * @implements hook_token_info().
 */
function mymodule_token_info() {
  $type = array(
    'name' => t('Mymodule'),
    'description' => t('Tokens related to individual instance of mymodule.'),
    'needs-data' => 'mymodule',
  );

  $mymodule['id'] = array(
    'name' => t("Entity ID"),
    'description' => t('The unique ID of the mymodule instance.'),
  );
  $mymodule['title'] = array(
    'name' => t("Title"),
    'description' => t('The name of the individual instance.'),
  );
  $mymodule['langcode'] = array(
    'name' => t('Language code'),
    'description' => t('The language code of the language translation.'),
  );
  $mymodule['url'] = array(
    'name' => t("URL"),
    'description' => t("The URL of the entity."),
  );
  $mymodule['edit-url'] = array(
    'name' => t("Edit URL"),
    'description' => t("The URL of the entity's edit page."),
  );

  return array(
    'types' => [
        'mymodule' => $type
    ],
    'tokens' => [
        'mymodule' => $mymodule
    ],
  );
}

/**
 * @implements hook_tokens().
 *
 * @param string $type
 * @param array $tokens
 * @param array $data
 * @param array $options
 * @param BubbleableMetadata $bubbleable_metadata
 *
 * @return array $replacements
 */
function mymodule_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $url_options = [
    'absolute' => TRUE
];
  if (isset($options['langcode'])) {
    $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
  }
  $replacements = array();

  if ($type == 'mymodule' && !empty($data['mymodule'])) {
    /** @var \Drupal\mymodule\Entity\Mymodule $mymodule */
    $mymodule = $data['mymodule'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'id':
          $replacements[$original] = $mymodule->id();
          break;

        case 'langcode':
          $replacements[$original] = $mymodule->language()->getId();
          break;

        case 'url':
          $replacements[$original] = $mymodule->url('canonical', $url_options);
          break;

        case 'edit-url':
          $replacements[$original] = $mymodule->url('edit-form', $url_options);
          break;

        case 'title':
          $replacements[$original] = $mymodule->getValue('title')[0]['value'];
          break;
      }
    }
  }

  return $replacements;
}

Adding the 'title' token to these functions has made the token show up as an option in /admin/config/search/path/patterns/add, and using it saves with no problems, but the "No new URL aliases to generate" problem persists.

Update 2: Pathauto alias generation is working after re-importing all of my entities and clearing Drupal cache.

  • Pathauto maintainer here. the token not existing sounds like a token.module issue, not sure, please share the base field definition. did you define tokens yourself or rely on the generic implementation in token.module? try latest dev version of both modules. not-generating could be because it has stored that no alias was generated. there is an issue in the queue to allow force-generating the aliases. – Berdir Sep 14 '16 at 21:08
  • Hi @Berdir, I did not define tokens myself, would this solve my issue? This is my base field definition: pastebin.com/epnzdt53 – saramm1 Sep 15 '16 at 12:39
  • Also forgot to add that I was already using the dev versions. – saramm1 Sep 15 '16 at 13:18
  • Well defining them yourself might solve it if the token then works. but that shouldn't be necessary. Can you open a bug report in token module about it? and include your whole entity type, so it is possible to try an reproduce it. – Berdir Sep 15 '16 at 13:43
  • I opened a ticket in the Token module a few weeks ago about this topic that looks like it hasn't been seen: drupal.org/node/2794343 – saramm1 Sep 15 '16 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.