Hi,

Does anyone know of a simple way to mark a field as "do not clone"? In my case, I have a set of nodes with a "Template" CCK checkbox, and the permissions set to only be viewable and editable by certain roles. When regular users clone the node, the CCK field is copied as well, so the new node shows up in the list of templates.

I suppose either respecting CCK field permissions (big can of worms I imagine), or simply excluding fields would work. Any better ideas I've missed?

Comments

pwolanin’s picture

You could use the alter hook to exclude certain fields.

deviantintegral’s picture

Status: Active » Fixed

Thanks; I've ended up doing that in a snippet like this:

    case 'simplenews_node_form':
      // Unset the template checkbox to convert this to a normal newsletter.
      if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'clone') {
        if (isset($form['#node']->field_newsletter_template[0]['value'])) {
          $form['#node']->field_newsletter_template[0]['value'] = 'normal';
        }
      }
      break;

Status: Fixed » Closed (fixed)

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

dinis’s picture

Just a quick 2c.

I required similar functionality; I setup a rule which instead of changing the code, it checks for new content being saved and inserts a null value into the requred CCK fields where the type is applicable.

Hope this helps someone :)

Danielle x

giorgio79’s picture

Status: Closed (fixed) » Active

Any chance of having sg built in the module where we could select fields to be excluded when cloning?
At the moment Danielle's solution only works if sg is saved, but as I understand in 6.x the node clone is edited first and even there I would like to empty some fields.

wgrunberg’s picture

@deviantintegral Do you put the snippet from #2 into your node-.tpl.php file?

deviantintegral’s picture

@wgrunberg, not it's in the site's custom code module in the hook_form_alter() implementation: http://api.drupal.org/api/function/hook_form_alter

wgrunberg’s picture

@deviantintegral Thanks!
Would you mind checking what I am doing wrong with the following? Non of the fiels are populated when clicking on the clone tab. Do I have to account for fieldsets (CCK "groups")?
I used these http://drupal.org/node/416986 guidelines to create my first custom module and used the Devel module to identify field names/arrays.

// $Id: metawiz.module

/**
* @file
* Modifications Specific to the USGIN Metadata Wizard site.
*/

// Alter forms through hook_form_alter()
function metawiz_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {

    // Alter some metadata fields in ct_metadata_core 
    case 'ct_metadata_core_node_form':
      // On clone (depends Node Clone module)
      if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'clone') {
	// Rename Title
        if (isset($form['#node']->title)) {
          $form['#node']->title = 'TEST';
        }
	// Empty the metadata UUID field.
        if (isset($form['#node']->field_core_met_uuid[0]['value'])) {
          $form['#node']->field_core_met_uuid[0]['value'] = 'TEST';
        }
	// Empty resource link
        if (isset($form['#node']->field_core_url[0]['url'])) {
          $form['#node']->field_core_url[0]['url'] = 'http://test.com';
        }
      }
      break;
  }
}

// omit PHP closing tag!
deviantintegral’s picture

As long as you're modifying the right values as shown by dumping the $form array, you should be doing it correctly. You only have to think about fieldsets if the parent element has #TREE set to TRUE. I would double-check to ensure that you have the right form ID.

cdale’s picture

Why are people using a form alter for this? Why not use hook_clone_node_alter()? Also, I'm sure the above code will not work most of the time. Maybe in rare cases.

From the post in #8, why not something like the following?


/**
 * Implementation of hook_clone_node_alter().
 */
function metawiz_clone_node_alter(&$node, $original_node, $method) {
  if ($node->type == 'ct_metadata_core') {
    if (isset($node->title)) {
      $node->title = 'TEST';
    }
    // Empty the metadata UUID field.
    if (isset($node->field_core_met_uuid[0]['value'])) {
      $node->field_core_met_uuid[0]['value'] = 'TEST';
    }
    // Empty resource link
    if (isset($node->field_core_url[0]['url'])) {
      $node->field_core_url[0]['url'] = 'http://test.com';
    }
  }
}

wgrunberg’s picture

Thanks cdale and deviantintegral for helping me!
hook_clone_node_alter() in #10 works nicely. Why wouldn't hook_form_alter() work? Is it specific to Drupal core?

wgrunberg’s picture

Status: Active » Closed (fixed)

Closed - thanks for the support.

deviantintegral’s picture

I'm not sure why using hook_form_alter() didn't work - it's currently working fine on a live site. Glad you got it working though!

cdale’s picture

hook_form_alter() *MIGHT* work for CCK fields if the modules form alter runs before CCK's form alter, and even then, it will only work when the save method for node_clone is prepopulate. However, altering the title like that in a hook_form_alter() will not work, you'd have to edit the values in the actual form field itself, not on #node. So basically, the code in #2 will work in some circumstances and configurations, but not in others, and the code in #8 will never fully work as the title can not be set in the way that is being done there. The code I've provided in #10 should always work regardless of method.

ladybug_3777’s picture

Issue summary: View changes

For anyone looking to do this with version 7.x, remember there is an example of the new structure in the clone.api.php file:

function hook_clone_node_alter(&$node, $context) {
  if ($context['original_node']->type == 'special') {
    $node->special = special_something();
  }
}

I was able to exclude the cloning of a specific field for all my content types by using that hook like this:

function YOURMODULENAME_clone_node_alter(&$node, $context) {
  //Exclude YOUR_CUSTOM_FIELD when cloning
  if (isset($node->YOUR_CUSTOM_FIELD)) {
    $node->YOUR_CUSTOM_FIELD = '';
  }
ladybug_3777’s picture

Hmmm... I'm actually thinking setting the fields to NULL might be better. I was looking at the code in the module for the function _clone_node_prepare and I noticed in there it is setting values to NULL.

If that's the case, this may work better (Perhaps someone that is more familiar can chime in on the proper way?)

function YOURMODULENAME_clone_node_alter(&$node, $context) {
  //Exclude YOUR_CUSTOM_FIELD when cloning
  if (isset($node->YOUR_CUSTOM_FIELD)) {
    $node->YOUR_CUSTOM_FIELD = NULL;
  }
alex72rm’s picture

The solution in #16 seems to be the best approach to solve the issue. Is it compatible with Drupal 7?
Furthermore, my issue is regarding pre-population of a cloned select list where the required field is prefilled automatically with the first element of the list, instead to leave it "empty & required". The prepopulation of certain fields when cloning is a big source of mistakes.