You are here

function hosting_get_site_by_url in Hosting 7.4

Same name and namespace in other branches
  1. 5 site/hosting_site.module \hosting_get_site_by_url()
  2. 6.2 site/hosting_site.module \hosting_get_site_by_url()
  3. 7.3 site/hosting_site.module \hosting_get_site_by_url()

Retrieves a site node based on the URL.

@todo Remove the db_query() and have only one EntityFieldQuery. The two different cases will be handled by adding different conditions/tags to it. A new tag will have to be added to match the title for aliases. These tags won't be necessary in Drupal 8 as JOINs are natively supported. See https://www.drupal.org/node/1882418 for details.

Parameters

string $url: The URL of the site.

boolean $loaded_object: Determines if a loaded site should be returned, or just the node ID. Defaults to TRUE.

Return value

If $loaded_object is TRUE, a fully loaded site node object. If $loaded_object is FALSE, the node ID of the site. In either case, if the site can't be found, FALSE is returned.

File

site/hosting_site.module, line 565
Contains hook implementations for Hosting site module.

Code

function hosting_get_site_by_url($url, $loaded_object = TRUE) {

  // If the Aliases feature is enabled, try and get the site by its alias too
  if (hosting_feature('alias')) {
    $nid = db_query("SELECT n.nid\n                       FROM {node} n\n                       JOIN {hosting_site} h\n                         ON n.nid = h.nid\n                  LEFT JOIN {hosting_site_alias} ha\n                         ON h.vid = ha.vid\n                      WHERE (n.title = :title OR ha.alias = :alias)\n                        AND n.type = :type\n                        AND NOT (h.status = :status)", array(
      ':title' => $url,
      ':alias' => $url,
      ':type' => 'site',
      ':status' => HOSTING_SITE_DELETED,
    ))
      ->fetchField();
  }
  else {

    // Get the list of node IDs whose title matches the name.
    $query = new EntityFieldQuery();
    $entities = $query
      ->entityCondition('entity_type', 'node')
      ->propertyCondition('type', 'site')
      ->propertyCondition('title', $url)
      ->addTag('site_not_deleted')
      ->range(0, 1)
      ->execute();

    // Get the ID of the single node, if there is one.
    if (!empty($entities['node'])) {

      // Fetch the node ID from the list and return it.
      $nids = array_keys($entities['node']);
      $nid = array_shift($nids);
    }
    else {
      $nid = FALSE;
    }
  }
  if ($nid) {
    return $loaded_object ? node_load($nid) : $nid;
  }
  return FALSE;
}