commit f5b0080b9e683632cfac9e2ebb85b621867a4339 Author: Erik Stielstra Date: Tue Jul 8 11:22:44 2014 +0200 #2194505-13 diff --git a/lib/Drupal/node_clone/Forms/CloneSettingsForm.php b/lib/Drupal/node_clone/Forms/CloneSettingsForm.php deleted file mode 100644 index a197726..0000000 --- a/lib/Drupal/node_clone/Forms/CloneSettingsForm.php +++ /dev/null @@ -1,117 +0,0 @@ -config('node_clone.settings'); - - $form['basic'] = array( - '#type' => 'fieldset', - '#title' => $this->t('General settings'), - ); - $form['basic']['clone_method'] = array( - '#type' => 'radios', - '#title' => $this->t('Method to use when cloning a node'), - '#options' => array('prepopulate' => $this->t('Pre-populate the node form fields'), 'save-edit' => $this->t('Save as a new node then edit')), - '#default_value' => $config->get('clone_method'), - ); - $form['basic']['clone_nodes_without_confirm'] = array( - '#type' => 'radios', - '#title' => $this->t('Confirmation mode when using the "Save as a new node then edit" method'), - '#default_value' => $config->get('clone_nodes_without_confirm'), - '#options' => array($this->t('Require confirmation (recommended)'), $this->t('Bypass confirmation')), - '#description' => $this->t('A new node may be saved immediately upon clicking the "clone" link when viewing a node, bypassing the normal confirmation form.'), - ); - $form['basic']['clone_menu_links'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Clone menu links'), - '#default_value' => $config->get('clone_menu_links'), - '#description' => $this->t('Should any menu link for a node also be cloned?'), - ]; - $form['basic']['clone_use_node_type_name'] = array( - '#type' => 'checkbox', - '#title' => $this->t('Use node type name in clone link'), - '#default_value' => $config->get('clone_use_node_type_name'), - '#description' => $this->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".'), - ); - - /* - * @TODO settings with variable names - */ - $form['publishing'] = array( - '#type' => 'fieldset', - '#title' => $this->t('Should the publishing options ( e.g. published, promoted, etc) be reset to the defaults?'), - ); - - $types = node_type_get_names(); - - $form['publishing']['clone_reset_options'] = array( - '#type' => 'checkboxes', - '#title' => $this->t('Reset publishing options'), - '#default_value' => $config->get('clone_reset_options') ? $config->get('clone_reset_options') : array(), - '#options' => $types, - '#description' => $this->t('Select any node types which should reset the publishing options when cloned. In other words, all node where publishing options should be as confured for the content type.'), - ); - - // Need the variable default key to be something that's never a valid node type. - $form['omit'] = array( - '#type' => 'fieldset', - '#title' => $this->t('Content types that are not to be cloned - omitted due to incompatibility'), - ); - $form['omit']['clone_omitted'] = array( - '#type' => 'checkboxes', - '#title' => $this->t('Omitted content types'), - '#default_value' => $config->get('clone_omitted') ? $config->get('clone_omitted') : array(), - '#options' => $types, - '#description' => $this->t('Select any node types which should never be cloned. In other words, all node types where cloning will fail.'), - ); - - return parent::buildForm($form, $form_state); - } - - /** - * Compares the submitted settings to the defaults and unsets any that are equal. This was we only store overrides. - */ - public function submitForm(array &$form, array &$form_state) { - - // Get config factory - $config = $this->config('node_clone.settings'); - - $form_values = $form_state['values']; - - $config - ->set('clone_method', $form_values['clone_method']) - ->set('clone_nodes_without_confirm', $form_values['clone_nodes_without_confirm']) - ->set('clone_menu_links', $form_values['clone_menu_links']) - ->set('clone_use_node_type_name', $form_values['clone_use_node_type_name']) - ->set('clone_reset_options', $form_values['clone_reset_options']) - ->set('clone_omitted', $form_values['clone_omitted']) - ->save(); - - parent::submitForm($form, $form_state); - - } -} diff --git a/lib/Drupal/node_clone/NodeCloneForm.php b/lib/Drupal/node_clone/NodeCloneForm.php deleted file mode 100644 index cac0a03..0000000 --- a/lib/Drupal/node_clone/NodeCloneForm.php +++ /dev/null @@ -1,138 +0,0 @@ -entity; - // Create clone of this entity - $this->original_node = clone $this->entity; - - // Make sure this node is treated as a new one. - $node->enforceIsNew(); - $node->setNewRevision(TRUE); - $node->setValue(array( - 'nid' => 0, - 'uuid' => \Drupal::service('uuid')->generate(), - 'vid' => 0, - 'log' => NULL, - 'created' => REQUEST_TIME, - 'uid' => $this->currentUser()->id(), - 'title' => $this->t('Clone of !title', array('!title' => $node->getTitle())), - ) - ); - - // clear comments from node - $node->setValue(array( - 'comment' => array( - 'cid' => '0', - 'last_comment_timestamp' => REQUEST_TIME, - 'last_comment_name' => NULL, - 'comment_count' => '0' - ) - ) - ); - - if ($this->config('node_clone.settings')->get('clone_reset_options')[$node->bundle()]) { - // Get default settings for this bundle - $type = entity_load('node_type', $node->bundle()); - $bundle_settings = $type->getModuleSettings('node'); - foreach (array('status', 'promote', 'sticky') as $key) { - $node->setValue(array( - $key => (int) $bundle_settings['options'][$key], - ) - ); - } - } - - // Clone menu_link if original node had one - if ($this->config('node_clone.settings')->get('clone_menu_links') && $menu_link = $this->cloneMenuLink($this->original_node)) { - $node->menu = $menu_link; - } - - // @todo Override other node properties. - } - - - - /** - * Overrides Drupal\node\NodeForm::form(). - */ - public function form(array $form, array &$form_state) { - /** @var \Drupal\node\NodeInterface $node */ - $node = $this->entity; - $form = parent::form($form, $form_state); - $form['#title'] = $this->t('Clone @type @title', array('@type' => node_get_type_label($node), '@title' => $node->label())); - // user should not unset 'Create new revision' - // unset($form['revision']); - // unset($form['log']); - - return $form; - } - - - /** - * Overrides Drupal\node\NodeForm::actions(). - */ - protected function actions(array $form, array &$form_state) { - $element = parent::actions($form, $form_state); - $element['delete']['#access'] = FALSE; - return $element; - } - - /** - * {@inheritdoc}. - */ - public function getFormID() { - return 'node_clone_edit_form'; - } - - /** - * Create a new menu link cloned from another node. - * - * Returns NULL if no existing link, or links are not to be cloned. - */ - - private function cloneMenuLink($node) { - // This will fetch the existing menu link if the node had one. - $link_path = 'node/' . $node->id(); - $mlid = \Drupal::entityQuery('menu_link')->condition('link_path', $link_path)->execute(); - if ($mlid) { - $menu_link = entity_load('menu_link', reset($mlid), FALSE); - $menu_link->enforceIsNew(TRUE); - $menu_link['link_title'] = t('Clone of !title', array('!title' => $menu_link['link_title'])); - $menu_link['link_path'] = NULL; - $menu_link['uuid'] = \Drupal::service('uuid')->generate(); - if (isset($menu_link['options']['attributes']['title'])) { - $menu_link['options']['attributes']['title'] = t('Clone of !title', array('!title' => $menu_link['options']['attributes']['title'])); - } - return $menu_link; - } - return NULL; - } -} diff --git a/src/Forms/CloneSettingsForm.php b/src/Forms/CloneSettingsForm.php new file mode 100644 index 0000000..a197726 --- /dev/null +++ b/src/Forms/CloneSettingsForm.php @@ -0,0 +1,117 @@ +config('node_clone.settings'); + + $form['basic'] = array( + '#type' => 'fieldset', + '#title' => $this->t('General settings'), + ); + $form['basic']['clone_method'] = array( + '#type' => 'radios', + '#title' => $this->t('Method to use when cloning a node'), + '#options' => array('prepopulate' => $this->t('Pre-populate the node form fields'), 'save-edit' => $this->t('Save as a new node then edit')), + '#default_value' => $config->get('clone_method'), + ); + $form['basic']['clone_nodes_without_confirm'] = array( + '#type' => 'radios', + '#title' => $this->t('Confirmation mode when using the "Save as a new node then edit" method'), + '#default_value' => $config->get('clone_nodes_without_confirm'), + '#options' => array($this->t('Require confirmation (recommended)'), $this->t('Bypass confirmation')), + '#description' => $this->t('A new node may be saved immediately upon clicking the "clone" link when viewing a node, bypassing the normal confirmation form.'), + ); + $form['basic']['clone_menu_links'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Clone menu links'), + '#default_value' => $config->get('clone_menu_links'), + '#description' => $this->t('Should any menu link for a node also be cloned?'), + ]; + $form['basic']['clone_use_node_type_name'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Use node type name in clone link'), + '#default_value' => $config->get('clone_use_node_type_name'), + '#description' => $this->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".'), + ); + + /* + * @TODO settings with variable names + */ + $form['publishing'] = array( + '#type' => 'fieldset', + '#title' => $this->t('Should the publishing options ( e.g. published, promoted, etc) be reset to the defaults?'), + ); + + $types = node_type_get_names(); + + $form['publishing']['clone_reset_options'] = array( + '#type' => 'checkboxes', + '#title' => $this->t('Reset publishing options'), + '#default_value' => $config->get('clone_reset_options') ? $config->get('clone_reset_options') : array(), + '#options' => $types, + '#description' => $this->t('Select any node types which should reset the publishing options when cloned. In other words, all node where publishing options should be as confured for the content type.'), + ); + + // Need the variable default key to be something that's never a valid node type. + $form['omit'] = array( + '#type' => 'fieldset', + '#title' => $this->t('Content types that are not to be cloned - omitted due to incompatibility'), + ); + $form['omit']['clone_omitted'] = array( + '#type' => 'checkboxes', + '#title' => $this->t('Omitted content types'), + '#default_value' => $config->get('clone_omitted') ? $config->get('clone_omitted') : array(), + '#options' => $types, + '#description' => $this->t('Select any node types which should never be cloned. In other words, all node types where cloning will fail.'), + ); + + return parent::buildForm($form, $form_state); + } + + /** + * Compares the submitted settings to the defaults and unsets any that are equal. This was we only store overrides. + */ + public function submitForm(array &$form, array &$form_state) { + + // Get config factory + $config = $this->config('node_clone.settings'); + + $form_values = $form_state['values']; + + $config + ->set('clone_method', $form_values['clone_method']) + ->set('clone_nodes_without_confirm', $form_values['clone_nodes_without_confirm']) + ->set('clone_menu_links', $form_values['clone_menu_links']) + ->set('clone_use_node_type_name', $form_values['clone_use_node_type_name']) + ->set('clone_reset_options', $form_values['clone_reset_options']) + ->set('clone_omitted', $form_values['clone_omitted']) + ->save(); + + parent::submitForm($form, $form_state); + + } +} diff --git a/src/NodeCloneForm.php b/src/NodeCloneForm.php new file mode 100644 index 0000000..cac0a03 --- /dev/null +++ b/src/NodeCloneForm.php @@ -0,0 +1,138 @@ +entity; + // Create clone of this entity + $this->original_node = clone $this->entity; + + // Make sure this node is treated as a new one. + $node->enforceIsNew(); + $node->setNewRevision(TRUE); + $node->setValue(array( + 'nid' => 0, + 'uuid' => \Drupal::service('uuid')->generate(), + 'vid' => 0, + 'log' => NULL, + 'created' => REQUEST_TIME, + 'uid' => $this->currentUser()->id(), + 'title' => $this->t('Clone of !title', array('!title' => $node->getTitle())), + ) + ); + + // clear comments from node + $node->setValue(array( + 'comment' => array( + 'cid' => '0', + 'last_comment_timestamp' => REQUEST_TIME, + 'last_comment_name' => NULL, + 'comment_count' => '0' + ) + ) + ); + + if ($this->config('node_clone.settings')->get('clone_reset_options')[$node->bundle()]) { + // Get default settings for this bundle + $type = entity_load('node_type', $node->bundle()); + $bundle_settings = $type->getModuleSettings('node'); + foreach (array('status', 'promote', 'sticky') as $key) { + $node->setValue(array( + $key => (int) $bundle_settings['options'][$key], + ) + ); + } + } + + // Clone menu_link if original node had one + if ($this->config('node_clone.settings')->get('clone_menu_links') && $menu_link = $this->cloneMenuLink($this->original_node)) { + $node->menu = $menu_link; + } + + // @todo Override other node properties. + } + + + + /** + * Overrides Drupal\node\NodeForm::form(). + */ + public function form(array $form, array &$form_state) { + /** @var \Drupal\node\NodeInterface $node */ + $node = $this->entity; + $form = parent::form($form, $form_state); + $form['#title'] = $this->t('Clone @type @title', array('@type' => node_get_type_label($node), '@title' => $node->label())); + // user should not unset 'Create new revision' + // unset($form['revision']); + // unset($form['log']); + + return $form; + } + + + /** + * Overrides Drupal\node\NodeForm::actions(). + */ + protected function actions(array $form, array &$form_state) { + $element = parent::actions($form, $form_state); + $element['delete']['#access'] = FALSE; + return $element; + } + + /** + * {@inheritdoc}. + */ + public function getFormID() { + return 'node_clone_edit_form'; + } + + /** + * Create a new menu link cloned from another node. + * + * Returns NULL if no existing link, or links are not to be cloned. + */ + + private function cloneMenuLink($node) { + // This will fetch the existing menu link if the node had one. + $link_path = 'node/' . $node->id(); + $mlid = \Drupal::entityQuery('menu_link')->condition('link_path', $link_path)->execute(); + if ($mlid) { + $menu_link = entity_load('menu_link', reset($mlid), FALSE); + $menu_link->enforceIsNew(TRUE); + $menu_link['link_title'] = t('Clone of !title', array('!title' => $menu_link['link_title'])); + $menu_link['link_path'] = NULL; + $menu_link['uuid'] = \Drupal::service('uuid')->generate(); + if (isset($menu_link['options']['attributes']['title'])) { + $menu_link['options']['attributes']['title'] = t('Clone of !title', array('!title' => $menu_link['options']['attributes']['title'])); + } + return $menu_link; + } + return NULL; + } +}