1.20.x path.module path_entity_insert(Entity $entity)

Implements hook_entity_insert().

File

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

Code

function path_entity_insert(Entity $entity) {
  $langcode = isset($entity->langcode) ? $entity->langcode : LANGUAGE_NONE;
  $uri = $entity->uri();

  // Only generate alais if entity has a uri
  if (!empty($uri)) {
    $path = array();
    if (isset($entity->path)) {
      $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' => '',
      'source' => $uri['path'],
      '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);
    }
    // Otherwise save any specified alias.
    elseif ($path['alias']) {
      path_save($path);
    }

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