Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions clone.module
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @file
* Allow users to make a copy of an item of content (a node) and then edit that copy.
Expand Down Expand Up @@ -47,6 +48,17 @@ function clone_menu() {
'file' => 'clone.pages.inc',
'description' => 'Allows users to clone (copy then edit) an existing node.',
);
if (!module_exists('node_clone_tab')) {
$config = config('clone.settings');
if ($config->get('clone_link') == 'tab') {
$type = MENU_LOCAL_TASK;
} else {
$type = MENU_LOCAL_ACTION;
}
} else {
$type = MENU_LOCAL_ACTION;
}
$item['type'] = MENU_LOCAL_TASK;
$items['node/%node/clone/%clone_token'] = array(
'access callback' => 'clone_access_cloning',
'access arguments' => array(1, TRUE, 3),
Expand All @@ -57,7 +69,7 @@ function clone_menu() {
'title arguments' => array(1),
'weight' => 5,
'file' => 'clone.pages.inc',
'type' => MENU_LOCAL_ACTION,
'type' => $type,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
return $items;
Expand Down Expand Up @@ -151,8 +163,8 @@ function clone_node_type_update($info) {
}

/**
* Implements hook_views_api.
*/
* Implements hook_views_api.
*/
function clone_views_api() {
return array(
'api' => 3,
Expand Down Expand Up @@ -193,8 +205,7 @@ function clone_form_node_form_alter(&$form, $form_state, $form_id) {
function clone_form_node_admin_content_alter(&$form, $form_state, $form_id) {
if (config_get('clone.settings', 'clone_method') == 'prepopulate') {
$destination = backdrop_get_destination();
}
else {
} else {
$destination = array();
}
// The property attribute changes in the $form array depending on the user role.
Expand Down Expand Up @@ -258,15 +269,15 @@ function clone_action_clone($original_node, $context) {
$node = _clone_node_prepare($original_node, $prefix_title);
if (isset($val['substitute_from']) && strlen($val['substitute_from']) && isset($val['substitute_to'])) {
$i = (!empty($val['substitute_case_insensitive']) ? 'i' : '');
$pattern = '#'. strtr($val['substitute_from'], array('#' => '\#')) . '#' . $i;
$pattern = '#' . strtr($val['substitute_from'], array('#' => '\#')) . '#' . $i;
foreach (array('title') as $property) {
$new = preg_replace($pattern, $val['substitute_to'], $node->{$property});
if ($new) {
$node->{$property} = $new;
}
}
foreach (array('body') as $property) {
foreach($node->{$property} as $lang => $row) {
foreach ($node->{$property} as $lang => $row) {
foreach ($row as $delta => $data) {
foreach (array('value', 'summary') as $key) {
if (isset($node->{$property}[$lang][$delta][$key])) {
Expand All @@ -287,8 +298,7 @@ function clone_action_clone($original_node, $context) {
if (module_exists('rules')) {
rules_invoke_event('clone_node', $node, $original_node);
}
}
else {
} else {
backdrop_set_message(t('Clone failed for %title : not permitted for nodes of type %type', array('%title' => $original_node->title, '%type' => $original_node->type)), 'warning');
}
}
Expand All @@ -297,28 +307,28 @@ function clone_action_clone($original_node, $context) {
* Action form.
*/
function clone_action_clone_form($context) {
$form['clone_context'] = array(
'#tree' => TRUE,
);
$form['clone_context']['prefix_title'] = array(
$form['clone_context'] = array(
'#tree' => TRUE,
);
$form['clone_context']['prefix_title'] = array(
'#title' => t('Prefix title'),
'#type' => 'checkbox',
'#description' => t('Should cloned node tiles be prefixed?'),
'#default_value' => isset($context['clone_context']['prefix_title']) ? $context['clone_context']['prefix_title'] : 1,
);
$form['clone_context']['substitute_from'] = array(
$form['clone_context']['substitute_from'] = array(
'#title' => t('Substitute from string'),
'#type' => 'textfield',
'#description' => t('String (or regex) to substitute from in title and body.'),
'#default_value' => isset($context['clone_context']['substitue_from']) ? $context['clone_context']['substitue_from'] : '',
);
$form['clone_context']['substitute_to'] = array(
$form['clone_context']['substitute_to'] = array(
'#title' => t('Substitute to string'),
'#type' => 'textfield',
'#description' => t('String (or regex) to substitute to in title and body.'),
'#default_value' => isset($context['clone_context']['substitue_to']) ? $context['clone_context']['substitue_to'] : '',
);
$form['clone_context']['substitute_case_insensitive'] = array(
$form['clone_context']['substitute_case_insensitive'] = array(
'#title' => t('Case insensitive substitution'),
'#type' => 'checkbox',
'#description' => t('Should the substituion match be case insensitive?'),
Expand Down
49 changes: 43 additions & 6 deletions clone.pages.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* Menu callback to configure module settings.
*/
function clone_settings($form, &$form_state) {
if (module_exists('node_clone_tab')) {
backdrop_set_message(t('Node Clone module already implements clone link as tab. You can disable Node Clone Tab module and set the link as tab in this form. Otherwise, Node Clone Tab will take over Node Clone.'), 'error');
}
$config = config('clone.settings');

$form['#config'] = 'clone.settings';
Expand Down Expand Up @@ -41,18 +44,31 @@ function clone_settings($form, &$form_state) {
'#default_value' => (int) $config->get('clone_menu_links'),
'#description' => t('Should any menu link for a node also be cloned?'),
);
$form['basic']['clone_use_node_type_name'] = array(
'#type' => 'checkbox',
'#title' => t('Use node type name in clone link'),
'#default_value' => (int) $config->get('clone_use_node_type_name'),
'#description' => t('If checked, the link to clone the node will contain the node type name, for example, "Clone this article", otherwise it will read "Clone content".'),
);

$form['basic']['clone_prefix_title'] = array(
'#type' => 'checkbox',
'#prefix' => '<label for="edit-clone-prefix-title">' . t('Prefix title') . '</label>',
'#title' => t('Prefix title'),
'#default_value' => (bool) $config->get('clone_prefix_title'),
'#description' => t('Should cloned node titles be prefixed?'),
);
if (!module_exists('node_clone_tab')) {
$form['basic']['clone_link'] = array(
'#type' => 'radios',
'#title' => t('How to show the clone link'),
'#options' => array('link' => t('As action link under tabs'), 'tab' => t('As tab')),
'#default_value' => $config->get('clone_link'),
);
}

$form['basic']['clone_use_node_type_name'] = array(
'#type' => 'checkbox',
'#prefix' => '<label for="edit-clone-use-node-type-name">' . t('Use node type name in clone link') . '</label>',
'#title' => t('Use node type name in clone link'),
'#default_value' => (int) $config->get('clone_use_node_type_name'),
'#description' => t('If checked, the link to clone the node will contain the node type name, for example, "Clone this article", otherwise it will read "Clone content".'),
);


$form['publishing'] = array(
'#type' => 'fieldset',
Expand Down Expand Up @@ -81,9 +97,30 @@ function clone_settings($form, &$form_state) {
'#description' => t('Select any node types which should <em>never</em> be cloned. In other words, all node types where cloning will fail.'),
);

$form['#submit'][] = 'node_clone_settings_submit';

return system_settings_form($form);
}

/**
* Custom submit handler for node_clone settings.
*/
function node_clone_settings_submit($form, &$form_state) {

// Clear all caches including the menu cache.
cache_flush('menu');
// Schedule the menu rebuild to happen at the end of the request.
backdrop_register_shutdown_function('node_clone_delayed_menu_rebuild');

}

/**
* Function to rebuild the menu during shutdown.
*/
function node_clone_delayed_menu_rebuild() {
menu_rebuild();
}

/**
* Menu callback: prompt the user to confirm the operation
*/
Expand Down
3 changes: 2 additions & 1 deletion config/clone.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"clone_omitted": [],
"clone_nodes_without_confirm": 0,
"clone_use_node_type_name": 0,
"clone_menu_links": 0
"clone_menu_links": 0,
"clone_link": "link"
}