I added a 'clone' link as a views field. It works the same way that the node edit views field works. It's working for me but should be tested.

Comments

pwolanin’s picture

Status: Needs review » Needs work

looks interesting, but it doesn't look like that code would work:

$data->type = $data->node_type;

should maybe be:

$node->type = $data->node_type;
jody lynn’s picture

StatusFileSize
new1.55 KB
jody lynn’s picture

Yes, you're right. New patch. (I mixed up the patch because on my version I also was changing the permissions around so that users can only clone their own posts, which perhaps should be a separate feature request.)

Lexandro@yandex.ru’s picture

And what about D-6? It doesn't work on it.

markus_petrux’s picture

This prolly needs a bit of work for D6 since Views have changed a little.

subscribing... and if I get a minute I'll try it.

markus_petrux’s picture

Version: 5.x-2.x-dev » 6.x-1.x-dev
Status: Needs work » Needs review

Well, here's a patch for D6. It's been build against HEAD checked out today.

The patch adds directory views with 2 files, and modifies clone.module. It's based on the same method used by views node implementation to add the edit link. Tested with latest Drupal 6.5 and Views 2.0-rc5.

Code appended to clone.module:

/**
 * Implementation of hook_views_api.
 */
function clone_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'clone') .'/views',
  );
}

This is file clone.views.inc:

/**
 * Implementation of hook_views_handlers()
 */
function clone_views_handlers() {
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'clone') . '/views',
    ),
    'handlers' => array(
      'views_handler_field_node_link_clone' => array(
        'parent' => 'views_handler_field_node_link',
      ),
    ),
  );
}

/**
 * Implementation of hook_views_data_alter()
 */
function clone_views_data_alter(&$views_data) {
  $views_data['node']['clone_node'] = array(
    'field' => array(
      'title' => t('Clone link'),
      'help' => t('Provide a simple link to clone the node.'),
      'handler' => 'views_handler_field_node_link_clone',
    ),
  );
}

This is file views_handler_field_node_link_clone.inc:

/**
 * Field handler to present a clone node link.
 */
class views_handler_field_node_link_clone extends views_handler_field_node_link {
  function construct() {
    parent::construct();
    $this->additional_fields['uid'] = 'uid';
    $this->additional_fields['type'] = 'type';
    $this->additional_fields['format'] = array('table' => 'node_revisions', 'field' => 'format');
  }

  function render($values) {
    // ensure user has access to clone this node.
    $node = new stdClass();
    $node->nid = $values->{$this->aliases['nid']};
    $node->uid = $values->{$this->aliases['uid']};
    $node->type = $values->{$this->aliases['type']};
    $node->format = $values->{$this->aliases['format']};
    $node->status = 1; // unpublished nodes ignore access control
    if (!clone_access($node)) {
      return;
    }

    $text = !empty($this->options['text']) ? $this->options['text'] : t('clone');
    return l($text, "node/$node->nid/clone", array('query' => drupal_get_destination()));
  }
}
markus_petrux’s picture

StatusFileSize
new2.76 KB

The patch.

pwolanin’s picture

StatusFileSize
new3.18 KB

that patch is no good, but happily the text pasted above seems usable.

Here's a proper patch. I am committing this to HEAD to get feedback - I gave it a quick test and could get the link to show/work.

Please re-open or open a new issue if this is broken...

pwolanin’s picture

Status: Needs review » Fixed

if you don't want to checkout from CVS, you should be able to downlaod a tarball from here before too long: http://drupal.org/node/96560

markus_petrux’s picture

Thank you, pwolanin. Just installed the new tar ball, and it's working for me. :)

Would you mind tell me what was wrong with my patch, so I can take into account next time, please?

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

  • Commit c1460e8 on 6.x-1.x, 7.x-1.x, master, 8.x-1.x by pwolanin:
    #220925 add views clone link, thanks to markus_petrux