1 path.module path_entity_update(Entity $entity)

Implements hook_entity_update().

File

core/modules/path/path.module, line 487
Enables users to customize URLs and provide automatic URL alias patterns.

Code

function path_entity_update(Entity $entity) {
  if (isset($entity->path)) {
    $langcode = isset($entity->langcode) ? $entity->langcode : LANGUAGE_NONE;
    $uri = $entity->uri();
    $path = $entity->path;
    $path['source'] = $uri['path'];

    // If no pattern exists for this entity type, default to a manual alias.
    $pattern = path_get_pattern_by_entity_type($entity->entityType(), $entity->bundle(), $langcode);
    $path += array(
      'pid' => NULL,
      'alias' => '',
      'langcode' => $langcode,
      'auto' => strlen($pattern) === 0 ? FALSE : TRUE,
    );

    // Trim whitespace and slashes from alias start and end.
    $path['alias'] = trim($path['alias'], " \t\n\r\0\x0B/");

    // Save an automatic alias if specified.
    if ($path['auto']) {
      module_load_include('inc', 'path');
      $path = path_save_automatic_entity_alias($entity);

      // Ideally this would be in hook_taxonomy_term_update() instead, but as
      // hook_entity_update() fires second, we have to include it directly here.
      if ($entity->entityType() === 'taxonomy_term') {
        // Generate new aliases for all children as well.
        foreach (taxonomy_get_tree($entity->vocabulary, $entity->tid, NULL, TRUE) as $subterm) {
          path_save_automatic_entity_alias($subterm);
        }
      }
    }
    else {
      // Delete old alias if user erased it.
      if (!empty($path['pid']) && empty($path['alias'])) {
        path_delete($path['pid']);
      }
      // Update or create any specified alias.
      if (!empty($path['alias'])) {
        path_save($path);
      }
    }

    // Save the updated path into the entity.
    if ($path) {
      $entity->path = $path;
    }
  }
}