You are here

function _entity_translation_validate_path_schemes in Entity Translation 7

Validate the given set of path schemes and remove invalid elements.

Each path scheme needs to fulfill the following requirements:

  • The 'path wildcard' key needs to be specified.
  • Every path (base/view/edit/translate) needs to contain the path wildcard.
  • The following path definitions (if specified) need to match existing menu items: 'base path', 'view path', 'edit path'.
  • The 'translate path' definition needs to have an existing parent menu item.

This function needs to be called once with a list of menu items passed as the last parameter, before it can be used for validation.

Parameters

$schemes: The array of path schemes.

$entity_type_label: The label of the current entity type. This is used in error messages.

$items: A list of menu items.

$warnings: (optional) Displays warnings when a path scheme does not validate.

2 calls to _entity_translation_validate_path_schemes()
entity_translation_admin_form in ./entity_translation.admin.inc
Builder function for the entity translation settings form.
entity_translation_menu_alter in ./entity_translation.module
Implements hook_menu_alter().
2 string references to '_entity_translation_validate_path_schemes'
entity_translation_admin_form in ./entity_translation.admin.inc
Builder function for the entity translation settings form.
entity_translation_menu_alter in ./entity_translation.module
Implements hook_menu_alter().

File

./entity_translation.module, line 279

Code

function _entity_translation_validate_path_schemes(&$schemes, $entity_type_label, $items = FALSE, $warnings = FALSE) {
  $paths =& drupal_static(__FUNCTION__);
  static $regex = '|%[^/]+|';
  if (!empty($items)) {

    // Some menu loaders in the item paths might have been altered: we need to
    // replace any menu loader with a plain % to check if base paths are still
    // compatible.
    $paths = array();
    foreach ($items as $path => $item) {
      $stripped_path = preg_replace($regex, '%', $path);
      $paths[$stripped_path] = $path;
    }
  }
  if (empty($schemes)) {
    return;
  }

  // Make sure we have a set of paths to validate the scheme against.
  if (empty($paths)) {

    // This should never happen.
    throw new Exception('The Entity Translation path scheme validation function has not been initialized properly.');
  }
  foreach ($schemes as $delta => &$scheme) {

    // Every path scheme needs to declare a path wildcard for the entity id.
    if (empty($scheme['path wildcard'])) {
      if ($warnings) {
        $t_args = array(
          '%scheme' => $delta,
          '%entity_type' => $entity_type_label,
        );
        watchdog('entity_translation', 'Entity Translation path scheme %scheme for entities of type %entity_type does not declare a path wildcard.', $t_args);
      }
      unset($schemes[$delta]);
      continue;
    }
    $wildcard = $scheme['path wildcard'];
    $validate_keys = array(
      'base path' => FALSE,
      'view path' => FALSE,
      'edit path' => FALSE,
      'translate path' => TRUE,
    );
    foreach ($validate_keys as $key => $check_parent) {
      if (isset($scheme[$key])) {
        $path = $scheme[$key];
        $parts = explode('/', $path);
        $scheme[$key . ' parts'] = $parts;

        // Check that the path contains the path wildcard. Required for
        // determining the position of the entity id in the path (see
        // entity_translation_menu_alter()).
        if (!in_array($wildcard, $parts)) {
          if ($warnings) {
            $t_args = array(
              '%path_key' => $key,
              '%entity_type' => $entity_type_label,
              '%wildcard' => $wildcard,
              '%path' => $path,
            );
            drupal_set_message(t('Invalid %path_key defined for entities of type %entity_type: entity wildcard %wildcard not found in %path.', $t_args), 'warning');
          }
          unset($scheme[$key]);
          continue;
        }

        // Remove the trailing path element for paths requiring an existing
        // parent menu item (i.e. the "translate path").
        $trailing_path_element = FALSE;
        if ($check_parent) {
          $trailing_path_element = array_pop($parts);
          $path = implode('/', $parts);
        }
        $stripped_path = preg_replace($regex, '%', $path);
        if (!isset($paths[$stripped_path])) {
          if ($warnings) {
            $t_args = array(
              '%path_key' => $key,
              '%entity_type' => $entity_type_label,
              '%path' => $path,
            );
            $msg = $check_parent ? t('Invalid %path_key defined for entities of type %entity_type: parent menu item not found for %path', $t_args) : t('Invalid %path_key defined for entities of type %entity_type: matching menu item not found for %path', $t_args);
            drupal_set_message($msg, 'warning');
          }
          unset($scheme[$key]);
        }
        else {
          $real_path = $paths[$stripped_path];
          $real_parts = explode('/', $real_path);

          // Restore previously removed trailing path element.
          if ($trailing_path_element) {
            $real_path .= '/' . $trailing_path_element;
            $real_parts[] = $trailing_path_element;
          }
          $scheme['real ' . $key] = $real_path;
          $scheme['real ' . $key . ' parts'] = $real_parts;
        }
      }
    }
  }
}