You are here

function pathauto_entity_presave in Pathauto 7

Implements hook_entity_presave().

File

./pathauto.module, line 382
Main file for the Pathauto module, which automatically generates aliases for content.

Code

function pathauto_entity_presave($entity, $entity_type) {
  if (isset($entity->path['pathauto']) && is_array($entity->path)) {

    // We must set an empty alias string for the path to prevent saving an
    // alias.
    $entity->path += array(
      'alias' => '',
    );
  }

  // About to be saved (before insert/update)
  if (!empty($entity->path['pathauto']) && isset($entity->path['old_alias']) && $entity->path['alias'] == '' && $entity->path['old_alias'] != '') {

    /**
     * There was an old alias, but when pathauto_perform_alias was checked
     * the javascript disabled the textbox which led to an empty value being
     * submitted. Restoring the old path-value here prevents the Path module
     * from deleting any old alias before Pathauto gets control.
     */
    $entity->path['alias'] = $entity->path['old_alias'];
  }

  // Help prevent errors with progromatically creating entities by defining
  // path['alias'] as an empty string.
  // @see http://drupal.org/node/1328180
  // @see http://drupal.org/node/1576552
  if (isset($entity->path['pathauto']) && !isset($entity->path['alias'])) {
    $entity->path['alias'] = '';
  }
}