Updated: Comment #0

Problem/Motivation

/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php contains calls to \Drupal and other procedural code.

Proposed resolution

Make it implement EntityControllerInterface and inject the request, the custom block storage controller, the cache service, the translation manager.
Remove calls to t(), entity_load(), entity_load_by_multiple(), \Drupal::request(), user_access(), cache_invalidate_tags()

Remaining tasks

Patch
Review

User interface changes

None

API changes

None

#1998658: Creating Custom Block with same name as existing block throws SQL error

CommentFileSizeAuthor
#70 interdiff.txt1.2 KBbenjy
#70 2060865_70.patch10.54 KBbenjy
#65 cb-modern.66.patch11.02 KBlarowlan
#62 custom_block-2060865-62.patch10.96 KBtim.plunkett
#54 modernize-custom-block-2060865-54.patch11.02 KBbenjy
#54 interdiff.txt947 bytesbenjy
#51 modernize-custom-block-2060865-51.patch11.01 KBbenjy
#47 interdiff.txt1.01 KBbenjy
#47 modernize-custom-block-2060865-46.patch11.46 KBbenjy
#44 interdiff.txt1.49 KBbenjy
#44 modernize-custom-block-2060865-44.patch11.46 KBbenjy
#42 modernize-custom-block-2060865-42_0.patch11.45 KBbenjy
#42 interdiff_0.txt1.96 KBbenjy
#37 modernize-custom-block-2060865-37.patch11.18 KBbenjy
#37 interdiff.txt2.05 KBbenjy
#34 interdiff.txt3.81 KBbenjy
#34 modernize-custom-block-2060865-34.patch11.35 KBbenjy
#31 interdiff.txt826 bytesbenjy
#31 customblockformcontroller-2060865-31.patch10.54 KBbenjy
#27 interdiff.txt1.47 KBbenjy
#27 customblockformcontroller-2060865-27.patch10.53 KBbenjy
#25 interdiff.txt1.53 KBbenjy
#25 customblockformcontroller-2060865-25.patch10.63 KBbenjy
#24 interdiff.txt674 bytesbenjy
#24 customblockformcontroller-2060865-24.patch10.45 KBbenjy
#22 customblockformcontroller-2060865-22.patch10.39 KBbenjy
#21 customblockformcontroller-2060865-19.patch10.38 KBbenjy
#21 interdiff.txt1.1 KBbenjy
#18 interdiff.txt2.19 KBbenjy
#18 customblockformcontroller-2060865-17.patch10.23 KBbenjy
#16 interdiff.txt10.84 KBbenjy
#16 customblockformcontroller-2060865-16.patch10.84 KBbenjy
#15 customblockformcontroller-2060865-15.patch10.84 KBbenjy
#15 interdiff.txt3.69 KBbenjy
#13 customblockformcontroller-2060865-13.patch8.48 KBbenjy
#13 interdiff.txt8.36 KBbenjy
#11 customblockformcontroller-2060865-11.patch5.25 KBbenjy
#11 interdiff.txt742 bytesbenjy
#9 customblockformcontroller-2060865-9.patch5.28 KBbenjy
#9 interdiff.txt4.38 KBbenjy
#7 customblockformcontroller-2060865-7.patch4.81 KBbenjy
#7 interdiff.txt689 bytesbenjy
#2 customblockformcontroller-2060865-2.patch4.8 KBbenjy

Comments

benjy’s picture

Assigned: Unassigned » benjy
benjy’s picture

Component: block.module » custom_block.module
Status: Active » Needs review
StatusFileSize
new4.8 KB

Initial patch attached. One part I wasn't sure about is:

Cache::invalidateTags(array('content' => TRUE));

Looking through core there are a couple of ways to achieve this.

Status: Needs review » Needs work

The last submitted patch, customblockformcontroller-2060865-2.patch, failed testing.

benjy’s picture

Status: Needs work » Needs review
larowlan’s picture

Status: Needs review » Needs work
@@ -8,13 +8,66 @@
+use Drupal\Core\Language\LanguageManager;

Is there an interface we can typehint instead for this (rather than a class)?

@@ -8,13 +8,66 @@
+   * The Entity Storage Controller.

Can we say 'The custom block storage controller' instead, ie be explicit about what controller.

There are still calls to t() that can be replaced with the StringTranslation service

tim.plunkett’s picture

Hold on for the t() insertion, #2059245: Add a FormBase class containing useful methods would ideally handle that.

benjy’s picture

Status: Needs work » Needs review
StatusFileSize
new689 bytes
new4.81 KB

@larowlan, I did look but couldn't find an interface. LanguageManager doesn't implement one and it seems it's used directly elsewhere in core, eg: \Drupal\Core\Datetime\Date

Comment updated.

Holding off on t() as suggested by tim.plunkett.

tim.plunkett’s picture

  1. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -8,13 +8,66 @@
    +use Symfony\Component\DependencyInjection\ContainerInterface;
    +use Drupal\Core\Cache\Cache;
    +use Symfony\Component\HttpFoundation\Request;
    

    Might as well put these in order

  2. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -8,13 +8,66 @@
    +  protected $storageController;
    ...
    +    $this->storageController = $storage_controller;
    

    Can we call this customBlockStorage instead? We're soon to be ditching the term 'controller', and its more descriptive

  3. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -8,13 +8,66 @@
    +   *   The Module Handler.
    ...
    +   *   The Request.
    ...
    +   *   The Language Manager.
    

    The capitalization is weird here.

  4. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -8,13 +8,66 @@
    +      $container->get('request'),
    

    You shouldn't inject the request, you can put it at the end of buildForm() and store it there.

  5. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -69,11 +122,10 @@ public function form(array $form, array &$form_state) {
    +    $language_configuration = $this->moduleHandler->invoke('language', 'get_default_configuration', array('custom_block', $block->type->value));
    

    language_get_default_configuration() is not a hook. This should just check moduleExists for language, and then call it. This is just an old style hack.

benjy’s picture

StatusFileSize
new4.38 KB
new5.28 KB

All issues above fixed.

Status: Needs review » Needs work

The last submitted patch, customblockformcontroller-2060865-9.patch, failed testing.

benjy’s picture

Status: Needs work » Needs review
StatusFileSize
new742 bytes
new5.25 KB

Sorry about the delay, somehow missed this failure.

tim.plunkett’s picture

Status: Needs review » Needs work
Issue tags: +FormInterface, +FormBase
  1. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -8,13 +8,62 @@
    +class CustomBlockFormController extends EntityFormControllerNG implements EntityControllerInterface {
    

    Form controllers no longer use EntityControllerInterface

  2. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -8,13 +8,62 @@
    +  /**
    +   * The request.
    +   *
    +   * @var \Symfony\Component\HttpFoundation\Request
    +   */
    +  protected $request;
    
    @@ -37,6 +86,15 @@ protected function prepareEntity() {
    +  public function buildForm(array $form, array &$form_state, Request $request = null) {
    +    $this->request = $request;
    +
    +    return parent::buildForm($form, $form_state);
    

    This is wrong, use $this->getRequest()

  3. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -8,13 +8,62 @@
    +    parent::__construct($module_handler);
    ...
    +      $container->get('module_handler'),
    

    Don't inject the module handler anymore

  4. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -8,13 +8,62 @@
    +  public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
    

    just use create(ContainerInterface $container) now

You also need to use $this->t() everywhere, and should remove Overrides/Implements for {@inheritdoc}

And please add a row to #2072251: [meta] Modernize forms to use FormBase for this.

benjy’s picture

Title: Make CustomBlockFormController use DI and remove procedural code where possible » Modernize custom_block.module forms
Status: Needs work » Needs review
StatusFileSize
new8.36 KB
new8.48 KB

Everything in #12 addressed. I replaced Overrides with inheritdoc apart from when there were additional comments.

Parent issue: #2072251: [meta] Modernize forms to use FormBase

tim.plunkett’s picture

CustomBlockDeleteForm::buildForm() still has request in it.

  1. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -8,13 +8,50 @@
    +  public function __construct(EntityStorageControllerInterface $custom_block_storage, LanguageManager $language_manager) {
    +
    +    $this->customBlockStorage = $custom_block_storage;
    

    Weird extra line here

  2. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -37,14 +74,14 @@ protected function prepareEntity() {
    -      drupal_set_title(t('Edit custom block %label', array('%label' => $block->label())), PASS_THROUGH);
    +      drupal_set_title($this->t('Edit custom block %label', array('%label' => $block->label())), PASS_THROUGH);
         }
    

    Use ['#title'] please

benjy’s picture

StatusFileSize
new3.69 KB
new10.84 KB

Fixed #14 and a few other calls to t() in CustomBlockDeleteForm.

I also noticed this above the title. // @todo Remove this once https://drupal.org/node/1981644 is in. That issue seems to have been resolved but I don't understand why we'd remove it?

benjy’s picture

StatusFileSize
new10.84 KB
new10.84 KB

Never mind, just realised the @todo was relative to drupal_set_title() new patch and interdiff attached. Just ignore #15

tim.plunkett’s picture

  1. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -8,13 +8,49 @@
    +use Drupal\Core\Controller\ControllerInterface;
    ...
    +class CustomBlockFormController extends EntityFormControllerNG implements ControllerInterface {
    

    EntityFormControllerNG already implements ContainerInjectionInterface, you can remove the ControllerInterface bit

  2. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -37,14 +73,13 @@ protected function prepareEntity() {
    -      drupal_set_title(t('Edit custom block %label', array('%label' => $block->label())), PASS_THROUGH);
    +      $form['#title'] = $this->t('Edit custom block %label', array('%label' => $block->label()));
    

    Can we get some screenshots of this in action with some chatacters like '&' in the title? Not sure how the PASS_THROUGH works with this.

  3. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -62,23 +97,25 @@ public function form(array $form, array &$form_state) {
    +    if($this->moduleHandler->moduleExists('language')) {
    

    Missing space after if

benjy’s picture

StatusFileSize
new10.23 KB
new2.19 KB

1. Is fixed, I removed the use statement and another unused one for DateTime.

2. PASS_THROUGH means that drupal_set_title() just leaves the passed input alone so the before and after are identical.

3. Added the space.

4. I also tidied up an indenting issue at the bottom of the file pointed out by CodeSniffer.

Status: Needs review » Needs work

The last submitted patch, customblockformcontroller-2060865-17.patch, failed testing.

jibran’s picture

+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
@@ -17,6 +21,36 @@
+    $this->languageManager = $language_manager;

languageManager declaration is missing.

benjy’s picture

Status: Needs work » Needs review
StatusFileSize
new1.1 KB
new10.38 KB

Fixed.

benjy’s picture

StatusFileSize
new10.39 KB

Straight reroll.

tim.plunkett’s picture

  1. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -7,9 +7,15 @@
     namespace Drupal\custom_block;
     
    +
    

    Unneened space

  2. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Form/CustomBlockDeleteForm.php
    @@ -41,8 +41,8 @@ public function getConfirmText() {
    +  public function buildForm(array $form, array &$form_state) {
    +    $form = parent::buildForm($form, $form_state);
    

    The parent::buildForm needs to be at the end of the method.

benjy’s picture

Issue summary: View changes
StatusFileSize
new10.45 KB
new674 bytes

New patch attached.

benjy’s picture

StatusFileSize
new10.63 KB
new1.53 KB

Moved buildForm() to bottom of method. Interdiff is with #22

Status: Needs review » Needs work

The last submitted patch, 24: customblockformcontroller-2060865-24.patch, failed testing.

benjy’s picture

Status: Needs work » Needs review
StatusFileSize
new10.53 KB
new1.47 KB

Let's try that again.

Status: Needs review » Needs work

The last submitted patch, 25: customblockformcontroller-2060865-25.patch, failed testing.

benjy’s picture

Status: Needs work » Needs review
dawehner’s picture

Status: Needs review » Needs work
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
@@ -17,6 +22,43 @@
 class CustomBlockFormController extends ContentEntityFormController {
...
+   *   The action storage controller.

Hey, I thought we talk about custom blocks :)

benjy’s picture

Status: Needs work » Needs review
StatusFileSize
new10.54 KB
new826 bytes

Thanks.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Interdiff looks fine!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -9,7 +9,12 @@
    +use Symfony\Component\HttpFoundation\Request;
    

    Not used

  2. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -225,11 +268,11 @@ public function delete(array $form, array &$form_state) {
    +        form_set_error('info', $this->t('A block with description %name already exists.', array(
    

    form_set_error is deprecated - should use the form builder service.

  3. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Form/CustomBlockDeleteForm.php
    @@ -41,8 +41,7 @@ public function getConfirmText() {
    -  public function buildForm(array $form, array &$form_state, Request $request = NULL) {
    -    $form = parent::buildForm($form, $form_state, $request);
    +  public function buildForm(array $form, array &$form_state) {
    

    Now you can remove the use Symfony\Component\HttpFoundation\Request; from the top of the CustomBlockDeleteForm.php file

  4. The are several usages of user_access() which is also deprecated and can be replaced with $this->currentUser()->hasPermission()
benjy’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new11.35 KB
new3.81 KB

Thanks, feedback done.

Status: Needs review » Needs work

The last submitted patch, 34: modernize-custom-block-2060865-34.patch, failed testing.

alexpott’s picture

@benjy chatted with @tim.plunkett on IRC about the formbuilder injection and we agreed that this was a bit much - we will probably add a formSetError method to formbase but until then we should just continue to use form_set_error() - so lets revert that from this patch - sorry!

benjy’s picture

Status: Needs work » Needs review
StatusFileSize
new2.05 KB
new11.18 KB

@alex, new patch attached with interdiff between #31. I did wonder if it made sense for formBase to have the formBuilder since it seems like it would be a common requirement across most forms.

Status: Needs review » Needs work

The last submitted patch, 37: modernize-custom-block-2060865-37.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 37: modernize-custom-block-2060865-37.patch, failed testing.

benjy’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 21: customblockformcontroller-2060865-19.patch, failed testing.

benjy’s picture

Status: Needs work » Needs review
StatusFileSize
new1.96 KB
new11.45 KB

New patch attached. All the tests started breaking after #2019055: Switch from field-level language fallback to entity-level language fallback was committed. Thanks to @alexpott for helping me fix that.

olli’s picture

  1. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -62,23 +107,25 @@ public function form(array $form, array &$form_state) {
    +      '#description' => $this->t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array('@overview' => url('admin/structure/block'))),
    

    Can we use $this->url() here?

  2. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
    @@ -225,11 +272,11 @@ public function delete(array $form, array &$form_state) {
           // @todo Inject this once https://drupal.org/node/2060865 is in.
    

    This @todo is for this issue.

benjy’s picture

StatusFileSize
new11.46 KB
new1.49 KB

Good catches.

tim.plunkett’s picture

+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
@@ -111,7 +111,7 @@ public function form(array $form, array &$form_state) {
+      '#description' => $this->t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array('@overview' => $this->url('admin/structure/block'))),

FormBase::url() takes a route name, if this doesn't fail its a bad thing. Should be $this->url('block.admin_display')

Status: Needs review » Needs work

The last submitted patch, 44: modernize-custom-block-2060865-44.patch, failed testing.

benjy’s picture

Status: Needs work » Needs review
StatusFileSize
new11.46 KB
new1.01 KB

Thanks Tim.

larowlan’s picture

+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
@@ -235,12 +282,11 @@ public function delete(array $form, array &$form_state) {
+        form_set_error('info', $this->t('A block with description %name already exists.', array(

This can use FormBuilder::setErrorByName() now, which can be injected

benjy’s picture

@larowlan, I did that in #34 and then Alex and Tim suggested against it in #36 because the formBuilder would probably be injected in formBase in the near future.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

In that case
Next time I'll read all the comments

benjy’s picture

StatusFileSize
new11.01 KB

Re-rolled.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 51: modernize-custom-block-2060865-51.patch, failed testing.

The last submitted patch, 51: modernize-custom-block-2060865-51.patch, failed testing.

benjy’s picture

Status: Needs work » Needs review
StatusFileSize
new947 bytes
new11.02 KB

Looks like how we get the entity manager changed.

Status: Needs review » Needs work

The last submitted patch, 54: modernize-custom-block-2060865-54.patch, failed testing.

benjy’s picture

Status: Needs work » Needs review
dawehner’s picture

+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
@@ -234,12 +283,11 @@ public function delete(array $form, array &$form_state) {
+        form_set_error('info', $form_state, $this->t('A block with description %name already exists.', array(

form_set_error can now be replaced by \Drupal::formBuilder()->setErrorByName($name, $form_state, $message);

benjy’s picture

@dawehner formBuilder has come up a few times in this thread. We took #36 as consensus?

tim.plunkett’s picture

Once #2145007: Convert form_set_error() in FormBase classes to use FormErrorInterface lands we won't want to inject it anyway, let's not do this here. Or get that in first, and then use $this->setFormError()

benjy’s picture

Back to RTBC or postponed then?

Personally I'd rather get this in and have a follow-up to fix all form_set_errors in block.module once #2145007: Convert form_set_error() in FormBase classes to use FormErrorInterface goes in.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

back again

tim.plunkett’s picture

StatusFileSize
new10.96 KB

Patch no longer applied because #2145007: Convert form_set_error() in FormBase classes to use FormErrorInterface changed form_set_error :)

Straight reroll.

webchick’s picture

62: custom_block-2060865-62.patch queued for re-testing.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 62: custom_block-2060865-62.patch, failed testing.

larowlan’s picture

Status: Needs work » Needs review
StatusFileSize
new11.02 KB

re-roll

andypost’s picture

Looks RTBC except one nitpick

+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
@@ -53,23 +99,25 @@ public function form(array $form, array &$form_state) {
+    if ($this->moduleHandler->moduleExists('language')) {
+      $language_configuration = language_get_default_configuration('custom_block', $block->bundle());
...
+      // Set the correct default language.
+      if ($block->isNew()) {
+        $language_default = $this->languageManager->getLanguage($language_configuration['langcode']);

Suppose better:
if ($this..('language') && $block->isNew()) {
...
}

benjy’s picture

Status: Needs review » Reviewed & tested by the community

$language_configuration is used again further down in the form so changing that if statement would change how it works.

xjm’s picture

65: cb-modern.66.patch queued for re-testing.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
@@ -97,14 +145,14 @@ public function form(array $form, array &$form_state) {
+      '#access' => $block->isNewRevision() || $this->currentUser()->hasPermission('administer blocks'),
...
+      '#access' => $this->currentUser()->hasPermission('administer blocks'),

$account is set to $this->currentUser at the top of the form function so these change are unnecessary. Unless we remove the creation of the $account variable.

benjy’s picture

Status: Needs work » Needs review
StatusFileSize
new10.54 KB
new1.2 KB

Fixed.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community
webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.