From 26548aa3ea8fdca4791676ce0e37e5e456f36ff9 Mon Sep 17 00:00:00 2001 From: Marco Villegas Date: Sat, 8 Jun 2013 00:56:11 -0500 Subject: [PATCH] Several changes to pass tests again. - Added type key to .info.yml file. - Modified block_example_block_view_alter() to reflect the new type of the second parameter. Also re-insert the behaviour from D7 version. - Used Drupal\block_example\Plugin\Block namespace instead of Drupal\block_example\Plugin\block\block. - Show how to use set cache. - Remove blockAccess() implementations since parent class does the same. - Use blockBuild() instead of build(), since that's the common case. - Changed tests to use 'setting[label]' on block settings form pages. - Also some minor code standard related changes. This will probably need another round of documentation changes, but it's running tests locally, let's see what bot thinks. --- block_example/block_example.info.yml | 3 +- block_example/block_example.module | 12 +-- .../Plugin/Block/ExampleConfigurableTextBlock.php | 67 ++++++++++++++++ .../Plugin/Block/ExampleEmptyBlock.php | 38 ++++++++++ .../Plugin/Block/ExampleUppercaseBlock.php | 36 +++++++++ .../block/block/ExampleConfigurableTextBlock.php | 80 -------------------- .../Plugin/block/block/ExampleEmptyBlock.php | 47 ------------ .../Plugin/block/block/ExampleUppercaseBlock.php | 44 ----------- .../block_example/Tests/BlockExampleTest.php | 12 ++- 9 files changed, 155 insertions(+), 184 deletions(-) create mode 100644 block_example/lib/Drupal/block_example/Plugin/Block/ExampleConfigurableTextBlock.php create mode 100644 block_example/lib/Drupal/block_example/Plugin/Block/ExampleEmptyBlock.php create mode 100644 block_example/lib/Drupal/block_example/Plugin/Block/ExampleUppercaseBlock.php delete mode 100644 block_example/lib/Drupal/block_example/Plugin/block/block/ExampleConfigurableTextBlock.php delete mode 100644 block_example/lib/Drupal/block_example/Plugin/block/block/ExampleEmptyBlock.php delete mode 100644 block_example/lib/Drupal/block_example/Plugin/block/block/ExampleUppercaseBlock.php diff --git a/block_example/block_example.info.yml b/block_example/block_example.info.yml index a67582c..c25900a 100644 --- a/block_example/block_example.info.yml +++ b/block_example/block_example.info.yml @@ -1,6 +1,7 @@ name: Block Example +type: module description: An example outlining how a module can define blocks. package: Example modules core: 8.x dependencies: -- block + - block diff --git a/block_example/block_example.module b/block_example/block_example.module index a65a9e2..d512d2f 100644 --- a/block_example/block_example.module +++ b/block_example/block_example.module @@ -59,13 +59,15 @@ function block_example_page() { * hook by creating a new block whose title has the string 'uppercase' in it * (set as title through the UI). */ - -// -function block_example_block_view_alter(array &$build, \Drupal\block\Plugin\Core\Entity\Block $block) { - if ($block->getPlugin() instanceof ExampleUppercaseBlock) { - $build['#title'] = drupal_strtoupper($build['#title']); +function block_example_block_view_alter(array &$build, \Drupal\block\BlockPluginInterface $block) { + // We'll search for the string 'uppercase'. + $definition = $block->getDefinition(); + if ((!empty($build['#configuration']['label']) && stristr($build['#configuration']['label'], 'uppercase')) || (!empty($definition['subject']) && stristr($definition['subject'], 'uppercase'))) { + // This will uppercase the block title. + $build['#configuration']['label'] = drupal_strtoupper($build['#configuration']['label']); } } + /** * @} End of "defgroup block_example". */ diff --git a/block_example/lib/Drupal/block_example/Plugin/Block/ExampleConfigurableTextBlock.php b/block_example/lib/Drupal/block_example/Plugin/Block/ExampleConfigurableTextBlock.php new file mode 100644 index 0000000..bf9f9a5 --- /dev/null +++ b/block_example/lib/Drupal/block_example/Plugin/Block/ExampleConfigurableTextBlock.php @@ -0,0 +1,67 @@ + t('A default value. This block was created at %time', array('%time' => date('c'))), + 'cache' => DRUPAL_CACHE_PER_ROLE, + ); + } + + /** + * Overrides \Drupal\block\BlockBase::blockForm(). + */ + public function blockForm($form, &$form_state) { + $form['block_example_string_text'] = array( + '#type' => 'textfield', + '#title' => t('Block contents'), + '#size' => 60, + '#description' => t('This text will appear in the example block.'), + '#default_value' => $this->configuration['block_example_string'], + ); + return $form; + } + + /** + * Overrides \Drupal\block\BlockBase::blockSubmit(). + */ + public function blockSubmit($form, &$form_state) { + $this->configuration['block_example_string'] = $form_state['values']['block_example_string_text']; + } + + /** + * Implements \Drupal\block\BlockBase::blockBuild(). + */ + public function blockBuild() { + return array( + '#type' => 'markup', + '#markup' => $this->configuration['block_example_string'], + ); + } + +} diff --git a/block_example/lib/Drupal/block_example/Plugin/Block/ExampleEmptyBlock.php b/block_example/lib/Drupal/block_example/Plugin/Block/ExampleEmptyBlock.php new file mode 100644 index 0000000..847441e --- /dev/null +++ b/block_example/lib/Drupal/block_example/Plugin/Block/ExampleEmptyBlock.php @@ -0,0 +1,38 @@ + 'sidebar_first', // Not usually provided. + * - 'visibility' => BLOCK_VISIBILITY_LISTED, // Not usually provided. + * - 'pages' => 'node/*', // Not usually provided here. + */ + +namespace Drupal\block_example\Plugin\Block; + +use Drupal\block\BlockBase; +use Drupal\Core\Annotation\Translation; +use Drupal\Component\Annotation\Plugin; + +/** + * Provides a 'Example: empty block' block. + * + * @Plugin( + * id = "example_empty", + * subject = @Translation("Example: empty block"), + * admin_label = @Translation("Example: empty block"), + * module = "block_example" + * ) + */ +class ExampleEmptyBlock extends BlockBase { + + /** + * Implements \Drupal\block\BlockBase::blockBuild(). + */ + public function blockBuild() { + return array(); + } + +} diff --git a/block_example/lib/Drupal/block_example/Plugin/Block/ExampleUppercaseBlock.php b/block_example/lib/Drupal/block_example/Plugin/Block/ExampleUppercaseBlock.php new file mode 100644 index 0000000..8874c96 --- /dev/null +++ b/block_example/lib/Drupal/block_example/Plugin/Block/ExampleUppercaseBlock.php @@ -0,0 +1,36 @@ + 'markup', + '#markup' => t("This block's title will be changed to uppercase. Any other block with 'uppercase' in the subject or title will also be altered. If you change this block's title through the UI to omit the word 'uppercase', it will still be altered to uppercase as the subject key has not been changed."), + ); + } + +} diff --git a/block_example/lib/Drupal/block_example/Plugin/block/block/ExampleConfigurableTextBlock.php b/block_example/lib/Drupal/block_example/Plugin/block/block/ExampleConfigurableTextBlock.php deleted file mode 100644 index fca3726..0000000 --- a/block_example/lib/Drupal/block_example/Plugin/block/block/ExampleConfigurableTextBlock.php +++ /dev/null @@ -1,80 +0,0 @@ - t('A default value. This block was created at %time', array('%time' => date('c'))), - ); - } - - /** - * Overrides \Drupal\block\BlockBase::blockForm(). - */ - public function blockForm($form, &$form_state) { - $form['block_example_string_text'] = array( - '#type' => 'textfield', - '#title' => t('Block contents'), - '#size' => 60, - '#description' => t('This text will appear in the example block.'), - '#default_value' => $this->configuration['block_example_string'], - ); - return $form; - } - - /** - * NOverrides \Drupal\block\BlockBase::blockSubmit(). - */ - public function blockSubmit($form, &$form_state) { - $this->configuration['block_example_string'] = $form_state['values']['block_example_string_text']; - } - - - /** - * Implements \Drupal\block\BlockBase::build(). - */ - public function build() { - return array( - '#type' => 'markup', - '#markup' => $this->configuration['block_example_string'], - ); - } - -} diff --git a/block_example/lib/Drupal/block_example/Plugin/block/block/ExampleEmptyBlock.php b/block_example/lib/Drupal/block_example/Plugin/block/block/ExampleEmptyBlock.php deleted file mode 100644 index 7ca7792..0000000 --- a/block_example/lib/Drupal/block_example/Plugin/block/block/ExampleEmptyBlock.php +++ /dev/null @@ -1,47 +0,0 @@ - 'sidebar_first', // Not usually provided. - 'visibility' => BLOCK_VISIBILITY_LISTED, // Not usually provided. - 'pages' => 'node/*', // Not usually provided here. -*/ - -/** - * Provides a 'Example: empty block' block. - * - * @Plugin( - * id = "example_empty", - * subject = @Translation("Example: empty block"), - * admin_label = @Translation("Example: empty block"), - * module = "block_example" - * ) - */ -class ExampleEmptyBlock extends BlockBase { - - /** - * Overrides \Drupal\block\BlockBase::blockAccess(). - */ - public function blockAccess() { - return true; - } - - /** - * Implements \Drupal\block\BlockBase::build(). - */ - public function build() { - return ; - } - -} diff --git a/block_example/lib/Drupal/block_example/Plugin/block/block/ExampleUppercaseBlock.php b/block_example/lib/Drupal/block_example/Plugin/block/block/ExampleUppercaseBlock.php deleted file mode 100644 index d6b6c98..0000000 --- a/block_example/lib/Drupal/block_example/Plugin/block/block/ExampleUppercaseBlock.php +++ /dev/null @@ -1,44 +0,0 @@ - 'markup', - '#markup' => t("This block's title will be changed to uppercase. Any other block with 'uppercase' in the subject or title will also be altered. If you change this block's title through the UI to omit the word 'uppercase', it will still be altered to uppercase as the subject key has not been changed."), - ); - } - -} diff --git a/block_example/lib/Drupal/block_example/Tests/BlockExampleTest.php b/block_example/lib/Drupal/block_example/Tests/BlockExampleTest.php index 6f7a95e..e67a470 100644 --- a/block_example/lib/Drupal/block_example/Tests/BlockExampleTest.php +++ b/block_example/lib/Drupal/block_example/Tests/BlockExampleTest.php @@ -55,26 +55,26 @@ class BlockExampleTest extends WebTestBase { // Add blocks // Create a new block and make sure it gets uppercased. $post = array( - 'label' => t('Title of first block (example_configurable_text)'), + 'settings[label]' => t('Title of first block (example_configurable_text)'), 'machine_name' => 'block_example_example_configurable_text', 'region' => 'sidebar_first', ); $this->drupalPost('admin/structure/block/add/example_configurable_text/'.$theme, $post, t('Save block')); $this->assertText(t('The block configuration has been saved.')); - $this->assertText($post['label']); + $this->assertText($post['settings[label]']); $this->drupalGet('admin/structure/block'); $this->assertLinkByHref(url('admin/structure/block/manage/'.$theme.'.block_example_example_configurable_text/configure')); $post = array( - 'label' => t('configurable block to be uppercased'), + 'settings[label]' => t('configurable block to be uppercased'), 'machine_name' => 'uppercased_block', 'region' => 'sidebar_first', ); $this->drupalPost('admin/structure/block/add/example_uppercase/'.$theme, $post, t('Save block')); $post = array( - 'label' => t('Example: empty block'), + 'settings[label]' => t('Example: empty block'), 'machine_name' => 'block_example_example_empty', 'region' => 'sidebar_first', ); @@ -92,13 +92,11 @@ class BlockExampleTest extends WebTestBase { // Change content of configurable text block $string = $this->randomName(); - $this->drupalPost('admin/structure/block/manage/'.$theme.'.block_example_example_configurable_text/configure%3Fdestination%3Dnode', array('block_example_string_text' => $string), t('Save block')); + $this->drupalPost('admin/structure/block/manage/'.$theme.'.block_example_example_configurable_text/configure%3Fdestination%3Dnode', array('settings[block_example_string_text]' => $string), t('Save block')); // Verify that new content is shown $this->drupalGet('/'); $this->assertRaw($string, 'Content of configurable text block successfully verified.'); - } } - -- 1.7.10.4