0

I have a content type "News". With URL aliases pattern and token the URL is like this: news/[node:title].

Is it possible to create two nodes on one submission with different URL aliases? Let's say on submitting on the news node, it will create two nodes with two URLs like this: The first one is news/[node:title] and the second will be news2/[node:title].

The whole point is not to create a two different content types, but one and the clone node will have a different URL aliases, so I can filter a new menu block with visible path news2/* and make other logic too.

0

You can use hook_node_presave copy the $node entity, change its URL and then save it with node_save or even better use entity_metadata_wrapper. I do not know if its recommended but i think it is a solution.

You can check if the node is new or not with $node->is_new i think

  • Hi, Gianni, Thank you for answer. OK, I've copied the $node with copy PHP function like this $node2 = copy $node; but now how can I set a different URL alias in $node2? – user2519032 Oct 20 '15 at 7:47
  • $clone_node = clone $node as seeing here clone PHP looks like; Are u using devel module? you can use dpm($node); or dpm($node_clone); to see its fields, properties and stuff and then cheing like $node->url = 'urlnew'; – Gianni Di Falco Oct 20 '15 at 7:51
  • I've managed to copy and change the url like this: $node2 = clone $node; and $node2->path['alias'] = 'news2/' . $node->nid; now left to see how can I save the clone node with node_save(). – user2519032 Oct 20 '15 at 7:54
  • Hmmm wait! i see a problem; original $node already has a nid? watch out for nids! – Gianni Di Falco Oct 20 '15 at 7:58
  • Yes, 'cos I've already created that node and I'm checking devel with updating the node. Yeah, good point I cannot use nid here, but how do I set a new url alias like this news2/[node:title] ? btw I don't see any $node->url in presave function. – user2519032 Oct 20 '15 at 8:01
0

Use hook_node_insert() where you can additionally create another node. I am not sure that the url alias will already be available. If not use the following code, found at Programmatically create alias using pathauto for node, to get the original URL alias and then edit it to match your criteria: module_load_include('inc', 'pathauto'); // include the inc file for Pathauto module

/*
pathauto_create_alias($module, $op, $source, $data, $type, $language)
@param $module: Your module name
@param $op: The operation (insert, update, return, bulkupdate)
@param $source: Internal Drupal path (i.e.: node/1)
@param $data: Addition data used to generate a pathalias, in this case the node object
@param $type: Type of content that will be used to match a pattern, defined in the backend url alias patterns
@param $language: If you have a multlingual website, you need to pass the language for which you want to generate the url alias
*/

pathauto_create_alias('your_module_name', 'insert', 'node/1', array('node' => $node), $node->type, $language->language); // Language is optional

Set the node alias and create a node with node_save().

  • I don't see any way to fetch the URL alias and change it programmatically with hook_form_alter(). Can you please expand your answer? – user2519032 Oct 20 '15 at 9:02
0

Multiple aliases in Drupal

[To put this in something more resembling English, we're talking about creating multiple URLs, more than one web address, to one page or piece of content on a Drupal site.]

It's actually very easy. Go to admin/path/add and add another alias for the node which you want multiply aliased.

(Look up the node/## first by going to the node, and checking the node/##/edit path– you'll have to be logged in to see the edit link.)

So if it's that simple, why would this need an explanatory page on Agaric Design?

Well, if you try to add your alias by editing an existing path, it doesn't work. (For instance if you first tried to redirect from a new path alias to an existing alias, which really doesn't work, and then edited it to point to a system path, any existing alias to that system path gets replaced.)

And seeing the fact that changing foo/bar to foo path alias to foo/bar to node/7 made foo to node/7 disappear-- well, you might logically assume that Drupal isn't letting you have multiple aliases to node 7.

Not true. Adding an alias, instead of editing an existing one to point to the system path, will give you multiple path aliases to the same system path

0

When you need to create a duplicate node on submission of one node. That means new node needs to be created after creation of first node. Drupal by default do not provide any hook which works for post node action. Even if you do something with node_presave() or node_insert() or node_update() that would always be buggy because all are working before db transaction ends. I have figured out a quick trick for you to overcome issues. And solving this question was also interesting. Thank you for such a question.

  1. Download a contrib module hook_post_action
  2. Install it and create a new custom module with following code, don't forget to add dependency of hook_post_action in .info file.

I have checked below code with both conditions when pathauto module is enabled and disabled. Any suggestions are welcome.

    /**
     * Implements hook_node_postinsert().
     */
    function MYMODULE_node_postinsert($node) {
      // We need to keep in mind that newly created duplicate node will also invoke this hook so use $_SESSION to avoid infinite looping.
      if (isset($_SESSION['nid_to_duplicate'])) {
        $path = path_load(array(
          'source' => 'node/' . $node->nid,
        ));
        if (!$path) {
          $path = array(
            'source' => 'node/' . $node->nid,
            'language' => $node->language,
          );
        }
        $previous_node_path = path_load(array(
          'source' => 'node/' . $_SESSION['nid_to_duplicate'],
        ));
        if (!$previous_node_path) {
          $alias_prefix = 'node/' . $_SESSION['nid_to_duplicate'];
        } else {
          $alias_prefix = $previous_node_path['alias'];
        }
        $alias_suffix = '-duplicate'; // IN YOUR CASE change it to '2' or whatever you want.
        $path['alias'] = $alias_prefix . $alias_suffix;
        path_save($path);
        unset($_SESSION['nid_to_duplicate']);
      }
      else {
        $_SESSION['nid_to_duplicate'] = $node->nid;
        $new_node = clone $node;
        $new_node->nid = NULL;
        $new_node->vid = NULL;
        if (isset($new_node->path)) {
          unset($new_node->path);
        }
        node_save($new_node);
      }
    }

And when you have pathauto module enabled, to match your scenario here is another set of code:

/**
 * Implements hook_node_postinsert().
 */
function MYMODULE_node_postinsert($node) {
  if (isset($_SESSION['nid_to_duplicate'])) {
    $path = path_load(array(
      'source' => 'node/' . $node->nid,
    ));
    // We are assuming that auto alias would be [node:content-type]/[node:title] and that is automatically created for the new node as well. Now change it.
    $alias_array = explode('/', $path['alias']);
    $path_suffix = '2'; // You can of course change it according to your need.
    // Change news to news2
    $alias_array[0] .= 2;
    // [node:title] token will be changed by pathauto, recreate it.
    $data = array(
      'node' => $node,
    );
    module_load_include('inc', 'pathauto');
    $alias_array[1] = token_replace('[node:title]', $data, array(
      'sanitize' => FALSE,
      'clear' => TRUE,
      'callback' => 'pathauto_clean_token_values',
      'language' => (object) array('language' => $node->language),
      'pathauto' => TRUE,
    ));
    $path['alias'] = implode('/', $alias_array);
    path_save($path);
    unset($_SESSION['nid_to_duplicate']);

  }
  else {
    $_SESSION['nid_to_duplicate'] = $node->nid;
    $new_node = clone $node;
    $new_node->nid = NULL;
    $new_node->vid = NULL;
    if (isset($new_node->path)) {
      unset($new_node->path);
    }
    node_save($new_node);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.