function chado_set_node_url

2.x tripal_core.chado_nodes.title_and_path.api.inc chado_set_node_url($node)
3.x tripal_core.chado_nodes.title_and_path.inc chado_set_node_url($node)

Set the URL for a given node.

Note: This makes the old URL completely invalid which breaks bookmarks. Furthermore, if someone attempts to go to an old URL they will get a white screen PDO error which is not very user friendly ;-) @todo handle re-directing for old URLs or at least ensure a page not found error is displayed.

Parameters

$node: The node to set the URL for. The node object must have at a minimum the 'nid' and 'type' properties, as well as the chado object (e.g. 'organism' for chado_organism node type, and 'feature' for chado_feature node type, etc.).

Return value

The URL alias that was set.

19 calls to chado_set_node_url()
chado_update_existing_node_urls in tripal_core/api/tripal_core.chado_nodes.title_and_path.api.inc
Tripal Job for updating all node URLs for a particular node type.
tripal_analysis_node_insert in tripal_analysis/includes/tripal_analysis.chado_node.inc
Implements hook_node_insert(). Acts on all content types.
tripal_analysis_node_update in tripal_analysis/includes/tripal_analysis.chado_node.inc
Implements hook_node_update(). Acts on all content types.
tripal_contact_node_insert in tripal_contact/includes/tripal_contact.chado_node.inc
Implements hook_node_insert(). Acts on all content types.
tripal_contact_node_update in tripal_contact/includes/tripal_contact.chado_node.inc
Implements hook_node_update(). Acts on all content types.

... See full list

File

tripal_core/api/tripal_core.chado_nodes.title_and_path.api.inc, line 582
Contains API functions to set titles and paths for all chado nodes

Code

function chado_set_node_url($node) {

  // Get the global variable determining the language of the current content.
  global $language_content;

  // First we need to get the URL alias.
  $url_alias = chado_get_node_url($node);

  // And remove any forward slashes since those wreak havok.
  $url_alias = preg_replace('/^\//', '', $url_alias);

  // Use the node to determine the source/original URL.
  $source_url = 'node/' . $node->nid;

  // Ensure that there are no spaces and other non-friendly characters are
  // sanitized.
  $url_alias = preg_replace('/\s+/', '-', $url_alias);

  // Only bother setting the alias if there is one.
  if (!empty($url_alias) AND $source_url != $url_alias) {

    // First we need to check if the alias already exists.
    $path = path_load(array('source' => $source_url, 'alias' => $url_alias));

    // Etierh there isn't an alias yet so we just create one.
    if (empty($path)) {
      $path = array(
        'source' => $source_url,
        'alias' => $url_alias
      );
      path_save($path);
    }
    // Or an Alias already exists but we would like to add a new one.
    elseif ($path['alias'] != $url_alias) {
      $path = array(
        'source' => $source_url,
        'alias' => $url_alias
      );
      path_save($path);
    }
    // If the Alias already exists we want to double check that it's for the
    // current node. Otherwise we should warn the administrator that the path
    // format they chose isn't unique.
    else {

      $num_aliases = db_query('SELECT count(*) as num_alias FROM {url_alias} WHERE alias=:alias AND source!=:source', 
      array(':alias' => $url_alias, ':source' => $source_url))->fetchField();

      // If there is an alias with a different source than warn the admin.
      if ($num_aliases > 0) {
        tripal_report_error('chado_node_api', TRIPAL_WARNING, 
        'URL Alias: The URL format for %content-type is not unique. Specifically, %alias was almost added multiple times.', 
        array(
          '%content-type' => $node->type,
          '%alias' => $url_alias,
        )
        );
      }
    }

  }

  return $url_alias;
}