Commit 6a6d48f5 authored by webchick's avatar webchick
Browse files

Issue #1987762 by damiankloip, vijaycs85, disasm, dawehner, tim.plunkett,...

Issue #1987762 by damiankloip, vijaycs85, disasm, dawehner, tim.plunkett, kim.pepper, googletorp, becw, wamilton: Convert node_add_page() to a new style controller.
parent a02a96df
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
use Drupal\Component\Utility\String; use Drupal\Component\Utility\String;
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\NodeTypeInterface; use Drupal\node\NodeTypeInterface;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
...@@ -19,19 +18,63 @@ ...@@ -19,19 +18,63 @@
class NodeController extends ControllerBase { class NodeController extends ControllerBase {
/** /**
* @todo Remove node_add_page(). * Displays add content links for available content types.
*
* Redirects to node/add/[type] if only one content type is available.
*
* @return array
* A render array for a list of the node types that can be added; however,
* if there is only one node type defined for the site, the function
* redirects to the node add page for that one node type and does not return
* at all.
*
* @see node_menu()
*/ */
public function addPage() { public function addPage() {
module_load_include('pages.inc', 'node'); $content = array();
return node_add_page();
// Only use node types the user has access to.
foreach ($this->entityManager()->getStorageController('node_type')->loadMultiple() as $type) {
if ($this->entityManager()->getAccessController('node')->createAccess($type->type)) {
$content[$type->type] = $type;
}
}
// Bypass the node/add listing if only one content type is available.
if (count($content) == 1) {
$type = array_shift($content);
return $this->redirect('node_add', array('node_type' => $type->type));
}
return array(
'#theme' => 'node_add_list',
'#content' => $content,
);
} }
/** /**
* @todo Remove node_add(). * Provides the node submission form.
*
* @param \Drupal\node\NodeTypeInterface $node_type
* The node type entity for the node.
*
* @return array
* A node submission form.
*/ */
public function add(EntityInterface $node_type) { public function add(NodeTypeInterface $node_type) {
module_load_include('pages.inc', 'node'); $account = $this->currentUser();
return node_add($node_type); $langcode = $this->moduleHandler()->invoke('language', 'get_default_langcode', array('node', $node_type->type));
$node = $this->entityManager()->getStorageController('node')->create(array(
'uid' => $account->id(),
'name' => $account->getUsername() ?: '',
'type' => $node_type->type,
'langcode' => $langcode ? $langcode : $this->languageManager()->getLanguage()->id,
));
$form = $this->entityManager()->getForm($node);
return $form;
} }
/** /**
...@@ -145,7 +188,7 @@ protected function buildPage(NodeInterface $node) { ...@@ -145,7 +188,7 @@ protected function buildPage(NodeInterface $node) {
* The page title. * The page title.
*/ */
public function addPageTitle(NodeTypeInterface $node_type) { public function addPageTitle(NodeTypeInterface $node_type) {
return $this->t('Create @name', array('@name' => $node_type->type)); return $this->t('Create @name', array('@name' => $node_type->name));
} }
} }
...@@ -7,7 +7,9 @@ ...@@ -7,7 +7,9 @@
namespace Drupal\node\Plugin\views\area; namespace Drupal\node\Plugin\views\area;
use Drupal\Core\Access\AccessManager;
use Drupal\views\Plugin\views\area\AreaPluginBase; use Drupal\views\Plugin\views\area\AreaPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Defines an area plugin to display a node/add link. * Defines an area plugin to display a node/add link.
...@@ -19,19 +21,57 @@ ...@@ -19,19 +21,57 @@
class ListingEmpty extends AreaPluginBase { class ListingEmpty extends AreaPluginBase {
/** /**
* Implements \Drupal\views\Plugin\views\area\AreaPluginBase::render(). * The access manager.
*
* @var \Drupal\Core\Access\AccessManager
*/
protected $accessManager;
/**
* Constructs a new ListingEmpty.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Access\AccessManager $access_manager
* The access manager.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, AccessManager $access_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->accessManager = $access_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('access_manager')
);
}
/**
* {@inheritdoc}
*/ */
public function render($empty = FALSE) { public function render($empty = FALSE) {
$account = \Drupal::currentUser();
if (!$empty || !empty($this->options['empty'])) { if (!$empty || !empty($this->options['empty'])) {
$element = array( $element = array(
'#theme' => 'links', '#theme' => 'links',
'#links' => array( '#links' => array(
array( array(
'href' => 'node/add', 'href' => 'node/add',
'title' => t('Add new content') 'title' => $this->t('Add new content'),
) ),
) , ),
'#access' => _node_add_access() '#access' => $this->accessManager->checkNamedRoute('node.add_page', array(), $account),
); );
return $element; return $element;
} }
......
...@@ -958,21 +958,6 @@ function _node_revision_access(EntityInterface $node, $op = 'view', $account = N ...@@ -958,21 +958,6 @@ function _node_revision_access(EntityInterface $node, $op = 'view', $account = N
return \Drupal::service('access_check.node.revision')->checkAccess($node, $account, $op, $langcode); return \Drupal::service('access_check.node.revision')->checkAccess($node, $account, $op, $langcode);
} }
/**
* Access callback: Checks whether the user has permission to add a node.
*
* @return
* TRUE if the user has add permission, otherwise FALSE.
*
* @see node_menu()
*
* @deprecated
* Use \Drupal::service('access_manager')->checkNamedRoute('node.add_page');
*/
function _node_add_access() {
return \Drupal::service('access_manager')->checkNamedRoute('node.add_page', array(), \Drupal::currentUser());
}
/** /**
* Implements hook_menu(). * Implements hook_menu().
*/ */
......
...@@ -13,37 +13,6 @@ ...@@ -13,37 +13,6 @@
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
/**
* Page callback: Displays add content links for available content types.
*
* Redirects to node/add/[type] if only one content type is available.
*
* @return array
* A render array for a list of the node types that can be added; however, if
* there is only one node type defined for the site, the function redirects
* to the node add page for that one node type and does not return at all.
*
* @see node_menu()
*
* @deprecated Use \Drupal\node\Controller\NodeController::addPage()
*/
function node_add_page() {
$content = array();
// Only use node types the user has access to.
$access_controller = Drupal::entityManager()->getAccessController('node');
foreach (node_type_get_types() as $type) {
if ($access_controller->createAccess($type->type)) {
$content[$type->type] = $type;
}
}
// Bypass the node/add listing if only one content type is available.
if (count($content) == 1) {
$type = array_shift($content);
return new RedirectResponse(url('node/add/' . $type->type, array('absolute' => TRUE)));
}
return array('#theme' => 'node_add_list', '#content' => $content);
}
/** /**
* Returns HTML for a list of available node types for node creation. * Returns HTML for a list of available node types for node creation.
* *
...@@ -72,34 +41,6 @@ function theme_node_add_list($variables) { ...@@ -72,34 +41,6 @@ function theme_node_add_list($variables) {
return $output; return $output;
} }
/**
* Page callback: Provides the node submission form.
*
* @param $node_type
* The node type object for the submitted node.
*
* @return array
* A node submission form.
*
* @see node_menu()
*
* @deprecated Use \Drupal\node\Controller\NodeController::add()
*/
function node_add($node_type) {
$user = \Drupal::currentUser();
$type = $node_type->type;
$langcode = module_invoke('language', 'get_default_langcode', 'node', $type);
$node = entity_create('node', array(
'uid' => $user->id(),
'name' => $user->getUsername(),
'type' => $type,
'langcode' => $langcode ? $langcode : language_default()->id,
));
return \Drupal::entityManager()->getForm($node);
}
/** /**
* Generates a node preview. * Generates a node preview.
* *
......
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