home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (path.rules.inc)

This example Drupal source code file (path.rules.inc) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, dst, function, if, input, label, module, node, path, php, replace, return, rules_clean_path, url

The path.rules.inc Drupal example source code

<?php
// $Id: path.rules.inc,v 1.1.2.7 2009/08/25 18:16:42 fago Exp $


/**
 * @file Rules integration for the path module
 *
 * @addtogroup rules
 * @{
 */

/**
 * Implementation of hook_rules_condition_info().
 */
function path_rules_condition_info() {
  return array(
    'rules_condition_url_has_alias' => array(
      'label' => t('Path has alias(es)'),
      'module' => 'Path',
      'eval input' => array('src'),
    ),
    'rules_condition_alias_exists' => array(
      'label' => t('URL alias exists'),
      'module' => 'Path',
      'eval input' => array('dst'),
    ),
  );
}

/**
 * Condition implementation: Check if the path has an alias
 */
function rules_condition_url_has_alias($settings) {
  return (bool)drupal_lookup_path('alias', $settings['src'], $settings['language']);
}

/**
 * Condition implementation: Check if the URL alias already exists
 */
function rules_condition_alias_exists($settings) {
  $dst = rules_clean_path($settings['dst'], $settings['replace']);
  return (bool)drupal_lookup_path('source', $dst, $settings['language']);
}

/**
 * Implementation of hook_rules_action_info().
 */
function path_rules_action_info() {
  return array(
    'rules_action_path_alias' => array(
      'label' => t('Create or delete an URL alias'),
      'module' => 'Path',
      'eval input' => array('src', 'dst'),
    ),
    'rules_action_node_path_alias' => array(
      'label' => t("Create or delete a content's URL alias"),
      'arguments' => array(
        'node' => array('type' => 'node', 'label' => t('Content')),
      ),
      'module' => 'Path',
      'eval input' => array('dst'),
    ),
  );
}

/**
 * Action Implementation
 */
function rules_action_path_alias($settings) {
  $dst = rules_clean_path($settings['dst'], $settings['replace']);
  // Only set the alias if the path alias isn't taken yet.
  if (!drupal_lookup_path('source', $dst)) {
    path_set_alias($settings['src'], $dst, NULL, $settings['language']);
  }
}

/**
 * Action Implementation
 */
function rules_action_node_path_alias($node, $settings) {
  $dst = rules_clean_path($settings['dst'], $settings['replace']);
  // Only set the alias if the path alias isn't taken yet.
  if (!drupal_lookup_path('source', $dst)) {
    $node->path = $dst;
    if (user_access('create url aliases') || user_access('administer url aliases')) {
      // Path module is going to save the alias on node save.
      return array('node' => $node);
    }
    else {
      path_set_alias('node/'. $node->nid, $node->path, isset($node->pid) ? $node->pid : NULL, isset($node->language) ? $node->language : '');
    }
  }
}

/**
 * Cleans the given path by replacing non ASCII characters with the given
 * $replace parameter
 *
 * Furthermore it allows customization by overriding the variable rules_clean_path in
 * the settings.php
 */
function rules_clean_path($path, $replace) {
  if ($replace) {
    $array = variable_get('rules_clean_path', array('/[^a-zA-Z0-9\/\-_]+/', $replace));
    $array[2] = $path;
    //replace it and remove trailing and leading replacement characters
    return trim(call_user_func_array('preg_replace', $array), $replace);
  }
  else {
    return $path;
  }
}

/**
 * @}
 */

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal path.rules.inc source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.