Skip to content
Snippets Groups Projects
Unverified Commit bd41f89e authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2693485 by juhog, paulocs, Gogowitsch, Hardik_Patel_12, YesCT, Mirroar,...

Issue #2693485 by juhog, paulocs, Gogowitsch, Hardik_Patel_12, YesCT, Mirroar, Lal_, ravi.shankar, abhisekmazumdar, tim.plunkett, longwave, esolitos, TravisCarden, lapek, jhuhta, adriancid, samiullah, adalbertov, cilefen, mohit1604, Abhijith S, John Cook, alexpott, dawehner, pameeela, larowlan, catch, leisurman: Content types are ordered by machine name on /node/add page (+ similar issues with other entities)

(cherry picked from commit f0eaa97e)
parent ea5a0587
Branches
Tags
5 merge requests!1285Issue #3240655 by elfakhar Aligne the active border,!541Issue #3123070: Fix 'PSR2.Classes.PropertyDeclaration.Underscore' coding standard,!463Resolve #3205025 "Class align center for media",!386Allow multiple vocabularies in the taxonomy filter,!308Issue #3118206: Remote media does not validate provider
......@@ -74,6 +74,7 @@ public function __construct(EntityStorageInterface $block_content_storage, Entit
*/
public function add(Request $request) {
$types = $this->blockContentTypeStorage->loadMultiple();
uasort($types, [$this->blockContentTypeStorage->getEntityType()->getClass(), 'sort']);
if ($types && count($types) == 1) {
$type = reset($types);
return $this->addForm($type, $request);
......
......@@ -91,20 +91,40 @@ protected function createBlockContent($title = FALSE, $bundle = 'basic', $save =
/**
* Creates a custom block type (bundle).
*
* @param string $label
* The block type label.
* @param array|string $values
* The value to create the block content type. If $values is an array
* it should be like: ['id' => 'foo', 'label' => 'Foo']. If $values
* is a string, it will be considered that it represents the label.
* @param bool $create_body
* Whether or not to create the body field
*
* @return \Drupal\block_content\Entity\BlockContentType
* Created custom block type.
*/
protected function createBlockContentType($label, $create_body = FALSE) {
$bundle = BlockContentType::create([
'id' => $label,
'label' => $label,
'revision' => FALSE,
]);
protected function createBlockContentType($values, $create_body = FALSE) {
if (is_array($values)) {
if (!isset($values['id'])) {
do {
$id = strtolower($this->randomMachineName(8));
} while (BlockContentType::load($id));
}
else {
$id = $values['id'];
}
$values += [
'id' => $id,
'label' => $id,
'revision' => FALSE,
];
$bundle = BlockContentType::create($values);
}
else {
$bundle = BlockContentType::create([
'id' => $values,
'label' => $values,
'revision' => FALSE,
]);
}
$bundle->save();
if ($create_body) {
block_content_add_body_field($bundle->id());
......
......@@ -50,6 +50,17 @@ protected function setUp(): void {
$this->drupalPlaceBlock('page_title_block');
}
/**
* Tests the order of the block content types on the add page.
*/
public function testBlockContentAddPageOrder() {
$this->createBlockContentType(['id' => 'bundle_1', 'label' => 'Bundle 1']);
$this->createBlockContentType(['id' => 'bundle_2', 'label' => 'Aaa Bundle 2']);
$this->drupalLogin($this->adminUser);
$this->drupalGet('block/add');
$this->assertSession()->pageTextMatches('/Aaa Bundle 2(.*)Bundle 1/');
}
/**
* Tests creating a block type programmatically and via a form.
*/
......
......@@ -80,6 +80,7 @@ public static function create(ContainerInterface $container) {
* type.
*/
public function addPage() {
$definition = $this->entityTypeManager()->getDefinition('node_type');
$build = [
'#theme' => 'node_add_list',
'#cache' => [
......@@ -89,8 +90,10 @@ public function addPage() {
$content = [];
$types = $this->entityTypeManager()->getStorage('node_type')->loadMultiple();
uasort($types, [$definition->getClass(), 'sort']);
// Only use node types the user has access to.
foreach ($this->entityTypeManager()->getStorage('node_type')->loadMultiple() as $type) {
foreach ($types as $type) {
$access = $this->entityTypeManager()->getAccessControlHandler('node')->createAccess($type->id(), NULL, [], TRUE);
if ($access->isAllowed()) {
$content[$type->id()] = $type;
......
......@@ -5,6 +5,7 @@
use Drupal\Core\Database\Database;
use Drupal\Core\Language\LanguageInterface;
use Drupal\node\Entity\Node;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
/**
* Create a node and test saving it.
......@@ -13,6 +14,8 @@
*/
class NodeCreationTest extends NodeTestBase {
use ContentTypeCreationTrait;
/**
* Modules to enable.
*
......@@ -42,6 +45,18 @@ protected function setUp(): void {
$this->drupalLogin($web_user);
}
/**
* Tests the order of the node types on the add page.
*/
public function testNodeAddPageOrder() {
$this->createContentType(['type' => 'bundle_1', 'name' => 'Bundle 1']);
$this->createContentType(['type' => 'bundle_2', 'name' => 'Aaa Bundle 2']);
$admin_content_types = $this->drupalCreateUser(['bypass node access']);
$this->drupalLogin($admin_content_types);
$this->drupalGet('node/add');
$this->assertSession()->pageTextMatches('/Aaa Bundle 2(.*)Bundle 1/');
}
/**
* Creates a "Basic page" node and verifies its consistency in the database.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment