I am building a site scraping script in feeds and I need to be able to populate the old URL to redirect to the newly created node.
It would be great to populate the redirection url(s) from here.

Comments

sanguis’s picture

Title: Add redirion URL in Feeds IMPORT » Add redirection URL in Feeds IMPORT
chrowe’s picture

I am also interested in this.
It looks like http://drupal.org/node/622700#mappingapi documents how to do this kind of thing.
There is also a similar module http://drupal.org/project/cck_redirection for D6 that is listed on http://drupal.org/node/856780

westie’s picture

Managed to get this working in own custom module for D7, however the cavet is that you have to run the import twice with the option to update the node. The reason for this is that I didnt know the nid to use when creating the Redirect as the node was not created yet. If anyone know a way round this I would be thankful :)

// Create a URL Redirect mapper in Feeds
function modulename_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  if ($entity_type == 'node') {
    $targets['help_custom_redirect'] = array(
      'name' => t('URL Redirect'),
      'description' => t('Creates a redirect for the value.'),
      'callback' => '_modulename_redirect_target',
    );
  }
}

// Callback function for the URL redirect
function _modulename_redirect_target($source, $entity, $target, $value) {

    // Use Database API to retrieve current redirect sources.
    $query = db_select('redirect', 'r');
    $query->fields('r', array('source'));
    $redirect_sources = $query->execute()->fetchCol();
    

    // URL to use in redirect ( in this example I take the content ID and add it to the URL structure)
    $url = '/app/answers/detail/a_id/' . $value;
  
    // Check node id exists and there is no current redirect set for it
    if (isset($entity->nid) && (!in_array($url, $redirect_sources))){
      $redirect = new stdClass();
      module_invoke(
        'redirect', 
        'object_prepare',
        $redirect, 
        array(
          'source' => $url,
          'source_options' => array(),
          'redirect' => 'node/' . $entity->nid,
          'redirect_options' => array(),
          'language' => LANGUAGE_NONE,
        )
      );
    module_invoke('redirect', 'save', $redirect);
    }
}

Also I think it would be a nice feature for the Redirect module to integrate with Feeds, if other people agree I can try get a patch going.

dubs’s picture

Yes, it would be great if this was included in Feeds. Thanks for adding this really useful code.

dshields’s picture

any progress on importing redirects with feeds?

dddbbb’s picture

This is a great idea. Feeds import of redirect info would be really handy. +1

osopolar’s picture

Issue summary: View changes

The problem with the code in #3 is, that its not working for new nodes, only for updates. But this could be easily done using hook_node_save() and hook_node_insert() to do the work.

I was currently doing this for D6 and path_redirect, but I guess its similar for D7 with the redirect module (instead of hook_nodeapi use hook_node_insert and hook_node_update()). Instead of path_redirect_load_by_source() you may use redirect_load_by_hash(redirect_hash($redirect)).

Code for Drupal 6:

/**
 * Implementation of hook_nodeapi().
 */
function my_news_nodeapi(&$node, $op, $arg = 0, $arg2 = 0) {
  if(('insert' == $op || 'update' ==$op) && 'news' == $node->type) {
    if (isset($node->feeds_my_news_redirect)) {
      // Save redirect.
      my_news_feeds_redirect_save($node);
    }
  }
  return;
}

/**
 * Implementation of hook_feeds_node_processor_targets_alter().
 */
function my_news_feeds_node_processor_targets_alter(&$targets, $content_type) {
  // Create a URL Redirect mapper in Feeds
  $targets['feeds_my_news_redirect'] = array(
    'name' => t('URL Redirect'),
    'description' => t('Creates a redirect for the value.'),
    'callback' => 'my_news_feeds_set_url_redirect',
  );
}

/**
 * Custom feeds-node-map to set url redirect for importet content.
 */
function my_news_feeds_set_url_redirect($node, $target, $value) {
  $node->$target = 'old/path/' . $value;
}

/**
 * Validate and save the path redirect.
 *
 * @see: path_redirect_validate_source_field()
 */
function my_news_feeds_redirect_save($node) {
  $path = 'node/' . $node->nid;

  // A redirect's 'from' cannot match any values from url_alias, it will cause
  // an infinite loop.
  if (db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s'", $node->feeds_my_news_redirect))) {
    return;
  }
  // Do not create redirects for valid paths.
  $menu_item = menu_get_item($node->feeds_my_news_redirect);
  if ($menu_item && $menu_item['page_callback'] != 'path_redirect_goto' && $node->feeds_my_news_redirect == $menu_item['path']) {
    return;
  }
  // Do not creat redirects if already exists.
  $redirect = path_redirect_load_by_source($node->feeds_my_news_redirect, $GLOBALS['language']->language);
  if (!empty($redirect)) {
    return;
  }

  $redirect = array(
    'source' => $node->feeds_my_news_redirect,
    'redirect' => $path,
  );
  path_redirect_save($redirect);
}
pere orga’s picture

Title: Add redirection URL in Feeds IMPORT » Add redirection URL in Feeds Import
Category: Feature request » Support request
RivkiTzipory’s picture

Feeds Node Import with Redirect

1
Install Redirect module
https://www.drupal.org/project/redirect

2
In the Article content Type add field to save the old_url

3
Map the old_url in the Importer
/admin/structure/feeds/article_import/mapping
4
Add hook_node_insert with code like this

function hook_node_insert($node) {

if($node->type == 'article' && isset($node->field_old_url_for_redirect))
{
// Use Database API to retrieve current redirect sources.
$query = db_select('redirect', 'r');
$query->fields('r', array('source'));
$redirect_sources = $query->execute()->fetchCol();

// The old path
$url = $node->field_old_url_for_redirect['und'][0]['value'];

// Check node id exists and there is no current redirect set for it
if (isset($node->nid) && (!in_array($url, $redirect_sources))){
$redirect = new stdClass();

module_invoke(
'redirect',
'object_prepare',
$redirect,
array(
'source' => $url,
'source_options' => array(),
'redirect' => 'node/' . $node->nid,
'redirect_options' => array(),
'language' => LANGUAGE_NONE,
)
);

module_invoke('redirect', 'save', $redirect);
}
}
}

zapo’s picture

Just tried out solution #9 and it works! Thanks @RivkiTzipory

aumcara’s picture

Hello RivkiTzipory and all,

I am recently in need to crawl a bit deeper in Drupal and I would love to apply your method.

But therefore, I need to learn about the hook operations .... and I just don't know where to start!

Do you mind to let me know from where I could start cause I am seriously interested by applying your method.

To apply hook is there a special module to install or so, I am just a bit lost.

Sorry, if my post is not supposed to be made here.

Thanks.

wylbur’s picture

Status: Active » Closed (outdated)

Another solution is to use the Path Redirect Import module to import redirect paths.

Closing this as Outdated as Drupal 7 is EOL.