From 8f028bb7d633d1c9116e73a1b8b8347f0a746142 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Sun, 30 Mar 2014 13:26:57 +0200
Subject: [PATCH] Issue #2225371 by mr.baileys, Wim Leers: Apply formatters and
 widgets to Custom Block base fields.

---
 .../CustomBlockFormController.php             | 12 +--------
 .../custom_block/Entity/CustomBlock.php       | 12 ++++++---
 .../Tests/CustomBlockCreationTest.php         | 26 +++++++++----------
 .../Tests/CustomBlockFieldTest.php            |  6 ++---
 .../Tests/CustomBlockListTest.php             |  4 +--
 .../Tests/CustomBlockTranslationUITest.php    | 14 ++++++++++
 .../Tests/CustomBlockTypeTest.php             | 10 +++----
 .../custom_block/Tests/PageEditTest.php       |  8 +++---
 8 files changed, 51 insertions(+), 41 deletions(-)

diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
index a476182aed19..73d1adc38323 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
@@ -96,16 +96,6 @@ public function form(array $form, array &$form_state) {
     // names.
     $form['#attributes']['class'][0] = drupal_html_class('block-' . $block->bundle() . '-form');
 
-    // Basic block information.
-    $form['info'] = array(
-      '#type' => 'textfield',
-      '#title' => $this->t('Block description'),
-      '#required' => TRUE,
-      '#default_value' => $block->label(),
-      '#weight' => -5,
-      '#description' => $this->t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array('@overview' => $this->url('block.admin_display'))),
-    );
-
     if ($this->moduleHandler->moduleExists('language')) {
       $language_configuration = language_get_default_configuration('custom_block', $block->bundle());
 
@@ -255,7 +245,7 @@ public function validateForm(array &$form, array &$form_state) {
       $exists = $this->customBlockStorage->loadByProperties(array('info' => $form_state['values']['info']));
       if (!empty($exists)) {
         $this->setFormError('info', $form_state, $this->t('A block with description %name already exists.', array(
-          '%name' => $form_state['values']['info'],
+          '%name' => $form_state['values']['info'][0]['value'],
         )));
       }
     }
diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php
index 4ad34d277adb..421e3b5ad5ff 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php
@@ -177,9 +177,15 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
       ->setDescription(t('The custom block language code.'));
 
     $fields['info'] = FieldDefinition::create('string')
-      ->setLabel(t('Subject'))
-      ->setDescription(t('The custom block name.'))
-      ->setRevisionable(TRUE);
+      ->setLabel(t('Block description'))
+      ->setDescription(t('A brief description of your block.'))
+      ->setRevisionable(TRUE)
+      ->setRequired(TRUE)
+      ->setDisplayOptions('form', array(
+        'type' => 'string',
+        'weight' => -5,
+      ))
+      ->setDisplayConfigurable('form', TRUE);
 
     $fields['type'] = FieldDefinition::create('entity_reference')
       ->setLabel(t('Block type'))
diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php
index 4111596feb20..167014e9de09 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php
@@ -60,14 +60,14 @@ public function testCustomBlockCreation() {
 
     // Create a block.
     $edit = array();
-    $edit['info'] = 'Test Block';
+    $edit['info[0][value]'] = 'Test Block';
     $edit['body[0][value]'] = $this->randomName(16);
     $this->drupalPostForm('block/add/basic', $edit, t('Save'));
 
     // Check that the Basic block has been created.
     $this->assertRaw(format_string('!block %name has been created.', array(
       '!block' => 'Basic block',
-      '%name' => $edit["info"]
+      '%name' => $edit['info[0][value]']
     )), 'Basic block created.');
 
     // Change the view mode.
@@ -82,7 +82,7 @@ public function testCustomBlockCreation() {
     $this->assertOption('edit-settings-custom-block-view-mode', 'default', 'The default view mode is available.');
 
     // Check that the block exists in the database.
-    $blocks = entity_load_multiple_by_properties('custom_block', array('info' => $edit['info']));
+    $blocks = entity_load_multiple_by_properties('custom_block', array('info' => $edit['info[0][value]']));
     $block = reset($blocks);
     $this->assertTrue($block, 'Custom Block found in database.');
 
@@ -92,7 +92,7 @@ public function testCustomBlockCreation() {
 
     // Check that the Basic block has been created.
     $this->assertRaw(format_string('A block with description %name already exists.', array(
-      '%name' => $edit["info"]
+      '%name' => $edit['info[0][value]']
     )));
     $this->assertResponse(200);
   }
@@ -105,7 +105,7 @@ public function testCustomBlockCreation() {
    */
   public function testDefaultCustomBlockCreation() {
     $edit = array();
-    $edit['info'] = $this->randomName(8);
+    $edit['info[0][value]'] = $this->randomName(8);
     $edit['body[0][value]'] = $this->randomName(16);
     // Don't pass the custom block type in the url so the default is forced.
     $this->drupalPostForm('block/add', $edit, t('Save'));
@@ -113,11 +113,11 @@ public function testDefaultCustomBlockCreation() {
     // Check that the block has been created and that it is a basic block.
     $this->assertRaw(format_string('!block %name has been created.', array(
       '!block' => 'Basic block',
-      '%name' => $edit["info"],
+      '%name' => $edit['info[0][value]'],
     )), 'Basic block created.');
 
     // Check that the block exists in the database.
-    $blocks = entity_load_multiple_by_properties('custom_block', array('info' => $edit['info']));
+    $blocks = entity_load_multiple_by_properties('custom_block', array('info' => $edit['info[0][value]']));
     $block = reset($blocks);
     $this->assertTrue($block, 'Default Custom Block found in database.');
   }
@@ -165,15 +165,15 @@ public function testFailedBlockCreation() {
   public function testBlockDelete() {
     // Create a block.
     $edit = array();
-    $edit['info'] = $this->randomName(8);
+    $edit['info[0][value]'] = $this->randomName(8);
     $body = $this->randomName(16);
     $edit['body[0][value]'] = $body;
     $this->drupalPostForm('block/add/basic', $edit, t('Save'));
 
     // Place the block.
     $instance = array(
-      'id' => drupal_strtolower($edit['info']),
-      'settings[label]' => $edit['info'],
+      'id' => drupal_strtolower($edit['info[0][value]']),
+      'settings[label]' => $edit['info[0][value]'],
       'region' => 'sidebar_first',
     );
     $block = entity_load('custom_block', 1);
@@ -194,11 +194,11 @@ public function testBlockDelete() {
     $this->assertText(format_plural(1, 'This will also remove 1 placed block instance.', 'This will also remove @count placed block instance.'));
 
     $this->drupalPostForm(NULL, array(), 'Delete');
-    $this->assertRaw(t('Custom block %name has been deleted.', array('%name' => $edit['info'])));
+    $this->assertRaw(t('Custom block %name has been deleted.', array('%name' => $edit['info[0][value]'])));
 
     // Create another block and force the plugin cache to flush.
     $edit2 = array();
-    $edit2['info'] = $this->randomName(8);
+    $edit2['info[0][value]'] = $this->randomName(8);
     $body2 = $this->randomName(16);
     $edit2['body[0][value]'] = $body2;
     $this->drupalPostForm('block/add/basic', $edit2, t('Save'));
@@ -208,7 +208,7 @@ public function testBlockDelete() {
     // Create another block with no instances, and test we don't get a
     // confirmation message about deleting instances.
     $edit3 = array();
-    $edit3['info'] = $this->randomName(8);
+    $edit3['info[0][value]'] = $this->randomName(8);
     $body = $this->randomName(16);
     $edit3['body[0][value]'] = $body;
     $this->drupalPostForm('block/add/basic', $edit3, t('Save'));
diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockFieldTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockFieldTest.php
index ffa719190826..5c7d46b4edf9 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockFieldTest.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockFieldTest.php
@@ -95,7 +95,7 @@ public function testBlockFields() {
     // Create a block.
     $this->drupalGet('block/add/link');
     $edit = array(
-      'info' => $this->randomName(8),
+      'info[0][value]' => $this->randomName(8),
       $this->field->getName() . '[0][url]' => 'http://example.com',
       $this->field->getName() . '[0][title]' => 'Example.com'
     );
@@ -104,8 +104,8 @@ public function testBlockFields() {
     $url = 'admin/structure/block/add/custom_block:' . $block->uuid() . '/' . \Drupal::config('system.theme')->get('default');
     // Place the block.
     $instance = array(
-      'id' => drupal_strtolower($edit['info']),
-      'settings[label]' => $edit['info'],
+      'id' => drupal_strtolower($edit['info[0][value]']),
+      'settings[label]' => $edit['info[0][value]'],
       'region' => 'sidebar_first',
     );
     $this->drupalPostForm($url, $instance, t('Save block'));
diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockListTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockListTest.php
index 5e5bd08b0b9e..82bcb4490897 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockListTest.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockListTest.php
@@ -63,7 +63,7 @@ public function testListing() {
     $this->clickLink($link_text);
     $this->assertResponse(200);
     $edit = array();
-    $edit['info'] = $label;
+    $edit['info[0][value]'] = $label;
     $edit['body[0][value]'] = $this->randomName(16);
     $this->drupalPostForm(NULL, $edit, t('Save'));
 
@@ -90,7 +90,7 @@ public function testListing() {
       $this->clickLink(t('Edit'));
       $this->assertResponse(200);
       $this->assertTitle(strip_tags(t('Edit custom block %label', array('%label' => $label)) . ' | Drupal'));
-      $edit = array('info' => $new_label);
+      $edit = array('info[0][value]' => $new_label);
       $this->drupalPostForm(NULL, $edit, t('Save'));
     }
     else {
diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTranslationUITest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTranslationUITest.php
index 39b7053e40d1..761936e97a1e 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTranslationUITest.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTranslationUITest.php
@@ -99,6 +99,20 @@ protected function getNewEntityValues($langcode) {
     return array('info' => $this->name) + parent::getNewEntityValues($langcode);
   }
 
+  /**
+   * Returns an edit array containing the values to be posted.
+   */
+  protected function getEditValues($values, $langcode, $new = FALSE) {
+    $edit = parent::getEditValues($values, $langcode, $new);
+    foreach ($edit as $property => $value) {
+      if ($property == 'info') {
+        $edit['info[0][value]'] = $value;
+        unset($edit[$property]);
+      }
+    }
+    return $edit;
+  }
+
   /**
    * Test that no metadata is stored for a disabled bundle.
    */
diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTypeTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTypeTest.php
index d107ad7b6b58..6c4a6817cf25 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTypeTest.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockTypeTest.php
@@ -178,15 +178,15 @@ public function testsCustomBlockAddTypes() {
           $this->clickLink('foo');
         }
         // Create a new block.
-        $edit = array('info' => $this->randomName(8));
+        $edit = array('info[0][value]' => $this->randomName(8));
         $this->drupalPostForm(NULL, $edit, t('Save'));
-        $blocks = $storage->loadByProperties(array('info' => $edit['info']));
+        $blocks = $storage->loadByProperties(array('info' => $edit['info[0][value]']));
         if (!empty($blocks)) {
           $block = reset($blocks);
           $destination = 'admin/structure/block/add/custom_block:' . $block->uuid() . '/' . $theme;
           $this->assertUrl(url($destination, array('absolute' => TRUE)));
           $this->drupalPostForm(NULL, array(), t('Save block'));
-          $this->assertUrl(url("admin/structure/block/list/$theme", array('absolute' => TRUE, 'query' => array('block-placement' => drupal_html_class($edit['info'])))));
+          $this->assertUrl(url("admin/structure/block/list/$theme", array('absolute' => TRUE, 'query' => array('block-placement' => drupal_html_class($edit['info[0][value]'])))));
         }
         else {
           $this->fail('Could not load created block.');
@@ -199,9 +199,9 @@ public function testsCustomBlockAddTypes() {
     $this->drupalGet('admin/structure/block/custom-blocks');
     $this->clickLink(t('Add custom block'));
     $this->clickLink('foo');
-    $edit = array('info' => $this->randomName(8));
+    $edit = array('info[0][value]' => $this->randomName(8));
     $this->drupalPostForm(NULL, $edit, t('Save'));
-    $blocks = $storage->loadByProperties(array('info' => $edit['info']));
+    $blocks = $storage->loadByProperties(array('info' => $edit['info[0][value]']));
     if (!empty($blocks)) {
       $destination = 'admin/structure/block/custom-blocks';
       $this->assertUrl(url($destination, array('absolute' => TRUE)));
diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/PageEditTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/PageEditTest.php
index 5bd711ce7239..7feb2c0687d1 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/PageEditTest.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/PageEditTest.php
@@ -31,16 +31,16 @@ public static function getInfo() {
   public function testPageEdit() {
     $this->drupalLogin($this->adminUser);
 
-    $title_key = 'info';
+    $title_key = 'info[0][value]';
     $body_key = 'body[0][value]';
     // Create block to edit.
     $edit = array();
-    $edit['info'] = drupal_strtolower($this->randomName(8));
+    $edit['info[0][value]'] = drupal_strtolower($this->randomName(8));
     $edit[$body_key] = $this->randomName(16);
     $this->drupalPostForm('block/add/basic', $edit, t('Save'));
 
     // Check that the block exists in the database.
-    $blocks = \Drupal::entityQuery('custom_block')->condition('info', $edit['info'])->execute();
+    $blocks = \Drupal::entityQuery('custom_block')->condition('info', $edit['info[0][value]'])->execute();
     $block = entity_load('custom_block', reset($blocks));
     $this->assertTrue($block, 'Custom block found in database.');
 
@@ -59,7 +59,7 @@ public function testPageEdit() {
     // Edit the same block, creating a new revision.
     $this->drupalGet("block/" . $block->id());
     $edit = array();
-    $edit['info'] = $this->randomName(8);
+    $edit['info[0][value]'] = $this->randomName(8);
     $edit[$body_key] = $this->randomName(16);
     $edit['revision'] = TRUE;
     $this->drupalPostForm(NULL, $edit, t('Save'));
-- 
GitLab