Commit a58f5d20 authored by git's avatar git Committed by Robin Monks

Issue #2254541 by mkdok: Plan for D8 port

parent ff50ac61
......@@ -3,4 +3,5 @@ description: Presents users with an option to create another node of the same ty
type: module
core: 8.x
package: User interface
version: 8.x-1.x
\ No newline at end of file
version: 8.x-1.x
configure: addanother.admin_config
......@@ -7,28 +7,8 @@
*/
/**
* Implements hook_install().
*/
function addanother_install() {
if (\Drupal::moduleHandler()->moduleExists('node')) {
$node_types = array_keys(node_type_get_names());
foreach ($node_types as $type_id) {
\Drupal::config('addanother.entity.node.' . $type_id)
->set('addanother_button', 0)
->set('addanother_message', 0)
->set('addanother_tab', 0)
->set('addanother_tab_edit', 0)
->save();
}
}
}
/**
* Implements hook_install().
* Implement hook_uninstall().
*/
function addanother_uninstall() {
$node_types = array_keys(node_type_get_names());
foreach ($node_types as $type_id) {
\Drupal::config('addanother.entity.node.' . $type_id)->delete();
}
}
\ No newline at end of file
\Drupal::service('config.factory')->getEditable('addanother')->delete();
}
addanother.redirect:
route_name: addanother.redirect
base_route: entity.node.canonical
title: Add another
weight: 20
addanother.add:
route_name: addanother.add
base_route: node.view
title: 'Add Another'
weight: 5
\ No newline at end of file
This diff is collapsed.
'administer add another':
title: Administer Add another
description: Configure content types for Add another.
'use add another':
title: Use Add another
description: Use the "Add another" link or button to create additional content.
addanother.add:
path: '/node/{node}/addanother'
addanother.admin_config:
path: 'admin/structure/types/addanother'
defaults:
_content: '\Drupal\addanother\Controller\addanotherController::add'
options:
_admin_route: TRUE
_form: '\Drupal\addanother\Form\AddAnotherSettingsForm'
_title: 'Add another'
_description: 'Modify which node types display the Add another message.'
requirements:
_permission: 'administer add another'
addanother.redirect:
path: 'node/{node}/addanother'
defaults:
_controller: '\Drupal\addanother\Controller\AddAnotherController::addAnotherGoTo'
_title: 'Add another redirect'
requirements:
_custom_access: '\Drupal\addanother\Controller\addanotherController::checkaddanotherAccess'
\ No newline at end of file
_custom_access: '\Drupal\addanother\Controller\AddAnotherController::addAnotherAccess'
options:
_admin_route: TRUE
<?php
/**
* @file
* Unit tests for Add another modu
*/
class AddanotherTestCase extends DrupalWebTestCase {
protected $privileged_user;
public static function getInfo() {
return array(
'name' => 'Add another',
'description' => 'Ensure that the Add another functionality works correctly.',
'group' => 'Add another',
);
}
public function setUp() {
parent::setUp('addanother');
}
public function testAddanother() {
$nodetype = $this->randomName(8);
variable_set("addanother_button_$nodetype", TRUE);
variable_set("addanother_message_$nodetype", TRUE);
variable_set("addanother_tab_$nodetype", TRUE);
variable_set("addanother_tab_edit_$nodetype", TRUE);
$settings = array(
'type' => $nodetype,
'name' => $nodetype,
);
$type = $this->drupalCreateContentType($settings);
$type_exists = db_query('SELECT 1 FROM {node_type} WHERE type = :type', array(':type' => $type->type))->fetchField();
$this->assertTrue($type_exists, 'The new content type has been created in the database.');
$web_user = $this->drupalCreateUser(array('bypass node access', 'administer content types', 'use add another', 'administer add another'));
$this->drupalLogin($web_user);
// Create a node.
$edit = array();
$langcode = LANGUAGE_NONE;
$edit["title"] = $this->randomName(8);
$edit["body[$langcode][0][value]"] = $this->randomName(16);
$this->drupalPost("node/add/$nodetype", $edit, t('Save'));
// Check that the node has been created
$this->assertRaw(t('!post %title has been created.', array('!post' => $nodetype, '%title' => $edit["title"])), t('Node created.'));
$this->assertText(t('Add another !type.', array('!type' => $nodetype)), t('Addanother message was presented.'));
$this->assertLink('Add another');
// Create a node.
$edit = array();
$langcode = LANGUAGE_NONE;
$edit["title"] = $this->randomName(8);
$edit["body[$langcode][0][value]"] = $this->randomName(16);
$this->drupalPost("node/add/$nodetype", $edit, t('Save and add another'));
// Check that the node has been created
$this->assertUrl("node/add/$nodetype");
}
}
default_button: TRUE
default_message: TRUE
default_tab: TRUE
default_tab_edit: TRUE
# Schema for the configuration files of the addanother module.
addanother.settings:
type: config_object
label: 'Node settings'
mapping:
default_button:
type: boolean
label: 'Default settings for displaying Add another button on node add form.'
default_message:
type: boolean
label: 'Default settings for displaying Add another message after node creation.'
default_tab:
type: boolean
label: 'Default settings for displaying Add another tab.'
default_tab_edit:
type: boolean
label: 'Default settings for displaying Add another tab on edit page.'
button:
type: sequence
label: 'Add another buttons'
sequence:
type: boolean
label: 'Add another button'
message:
type: sequence
label: 'Add another messages'
sequence:
type: boolean
label: 'Add another message'
tab:
type: sequence
label: 'Add another tabs'
sequence:
type: boolean
label: 'Add another tab'
tab_edit:
type: sequence
label: 'Add another edit tabs'
sequence:
type: boolean
label: 'Add another edit tab'
<?php
/**
* @file
* Contains \Drupal\menu\Controller\MenuController.
*/
namespace Drupal\addanother\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\node\NodeInterface;
use Drupal\Core\Access\AccessInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Returns responses for Menu routes.
*/
class addanotherController extends ControllerBase {
//Takes the user to the node creation page for the type of a given node.
public function add(NodeInterface $node) {
return $this->redirect('node.add', array('node_type' => $node->getType()));
}
/**
* Checks access for the subtree controller.
*/
public function checkaddanotherAccess(Request $request, NodeInterface $node) {
$config_values = \Drupal::config('addanother.entity.node.' . $node->getType())->get();
if (!($node->access('create', $this->currentUser()))) {
return AccessInterface::DENY;
}
if (arg(2) == "edit" && !$config_values['addanother_tab_edit']) {
return AccessInterface::DENY;
}
if ($node && $config_values['addanother_tab'] && $this->currentUser()->hasPermission('use add another')) {
return AccessInterface::ALLOW;
}
return AccessInterface::DENY;
}
}
\ No newline at end of file
<?php
/**
* @file
* Contains \Drupal\addanother\Controller\AddAnotherController.
*/
namespace Drupal\addanother\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\node\NodeInterface;
/**
* Controller routines for Add another routes.
*/
class AddAnotherController extends ControllerBase {
/**
* Takes the user to the node creation page for the type of a given node.
*/
public function addAnotherGoTo(NodeInterface $node) {
return $this->redirect('node.add', ['node_type' => str_replace('_', '-', $node->getType())]);
}
/**
* Takes the user to the node creation page for the type of a given node.
*/
public function addAnotherAccess(NodeInterface $node) {
if (!$node->access('create')) return AccessResult::forbidden();
$config = \Drupal::config('addanother.settings');
$account = \Drupal::currentUser();
$type = $node->getType();
if (\Drupal::routeMatch()->getRouteName() == 'entity.node.edit_form' &&
!$config->get('tab_edit.' . $type)) {
return AccessResult::forbidden();
}
if ($config->get('tab.' . $type) &&
$account->hasPermission('use add another')) {
return AccessResult::allowed();
}
return AccessResult::forbidden();
}
}
<?php
/**
* @file
* Contains \Drupal\addanother\Form\AddAnotherSettingsForm.
*/
namespace Drupal\addanother\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure Add another settings for this site.
*/
class AddAnotherSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'addanother_admin_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['addanother.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('addanother.settings');
$form['addanother_display'] = array(
'#type' => 'fieldset',
'#title' => $this->t('Default settings for newly created content types'),
);
$form['addanother_display']['default_button'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Enable <i>Display Add another button on node add form</i> for new content types.'),
'#default_value' => $config->get('default_button'),
);
$form['addanother_display']['default_message'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Enable <i>Display the Add another message after node creation</i> for new content types.'),
'#default_value' => $config->get('default_message'),
);
$form['addanother_display']['default_tab'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Enable <i>Display the Add another tab</i> for new content types.'),
'#default_value' => $config->get('default_tab'),
);
$form['addanother_display']['default_tab_edit'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Enable <i>Also display the Add another tab on edit page</i> for new content types.'),
'#default_value' => $config->get('default_tab_edit'),
);
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('addanother.settings')
->set('default_button', $form_state->getValue('default_button'))
->set('default_message', $form_state->getValue('default_message'))
->set('default_tab', $form_state->getValue('default_tab'))
->set('default_tab_edit', $form_state->getValue('default_tab_edit'))
->save();
parent::submitForm($form, $form_state);
}
}
<?php
/**
* @file
* SimpleTest-based functional test for Add another module.
*/
namespace Drupal\addanother\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests Addanother functionality.
*
* @group Addanother
*/
class AddAnotherTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
static public $modules = array('addanother');
/**
* The installation profile to use with this test.
*
* @var string
*/
protected $profile = 'minimal';
/**
* Tests Database Logging module functionality through interfaces.
*
* First creates content type, then logs in users, then creates nodes,
* and finally tests Addanother module functionality through user interface.
*/
public function testAddanother() {
$node_type = $this->randomMachineName(8);
$config = \Drupal::service('config.factory')->getEditable('addanother.settings');
$config
->set('button.' . $node_type, TRUE)
->set('message.' . $node_type, TRUE)
->set('tab.' . $node_type, TRUE)
->set('tab_edit.' . $node_type, TRUE)
->save();
$settings = array(
'type' => $node_type,
'name' => $node_type,
);
$this->drupalCreateContentType($settings);
$web_user = $this->drupalCreateUser(array('bypass node access', 'administer content types', 'use add another', 'administer add another'));
$this->drupalLogin($web_user);
// Create a node.
$edit = array();
$edit['title[0][value]'] = $this->randomMachineName(8);
$edit['body[0][value]'] = $this->randomMachineName(16);
$this->drupalPostForm("node/add/$node_type", $edit, t('Save'));
// Check that the node has been created
$this->assertText(t('@post @title has been created.', array(
'@post' => $node_type,
'@title' => $edit['title[0][value]']
)), 'Node created.');
$this->assertText(t('You may add another @type.', array('@type' => $node_type)), 'Addanother message was presented.');
$this->assertLink('Add another');
// Create a node.
$edit = array();
$edit['title[0][value]'] = $this->randomMachineName(8);
$edit['body[0][value]'] = $this->randomMachineName(16);
$this->drupalPostForm("node/add/$node_type", $edit, t('Save and add another'));
// Check that the node has been created
$this->assertUrl("node/add/$node_type");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment