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
Related Issues
#1998658: Creating Custom Block with same name as existing block throws SQL error
| Comment | File | Size | Author |
|---|---|---|---|
| #70 | interdiff.txt | 1.2 KB | benjy |
| #70 | 2060865_70.patch | 10.54 KB | benjy |
| #65 | cb-modern.66.patch | 11.02 KB | larowlan |
| #62 | custom_block-2060865-62.patch | 10.96 KB | tim.plunkett |
Comments
Comment #1
benjy commentedComment #2
benjy commentedInitial patch attached. One part I wasn't sure about is:
Looking through core there are a couple of ways to achieve this.
Comment #4
benjy commented#2: customblockformcontroller-2060865-2.patch queued for re-testing.
Comment #5
larowlanIs there an interface we can typehint instead for this (rather than a class)?
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
Comment #6
tim.plunkettHold on for the t() insertion, #2059245: Add a FormBase class containing useful methods would ideally handle that.
Comment #7
benjy commented@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.
Comment #8
tim.plunkettMight as well put these in order
Can we call this customBlockStorage instead? We're soon to be ditching the term 'controller', and its more descriptive
The capitalization is weird here.
You shouldn't inject the request, you can put it at the end of buildForm() and store it there.
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.
Comment #9
benjy commentedAll issues above fixed.
Comment #11
benjy commentedSorry about the delay, somehow missed this failure.
Comment #12
tim.plunkettForm controllers no longer use EntityControllerInterface
This is wrong, use $this->getRequest()
Don't inject the module handler anymore
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.
Comment #13
benjy commentedEverything in #12 addressed. I replaced Overrides with inheritdoc apart from when there were additional comments.
Parent issue: #2072251: [meta] Modernize forms to use FormBase
Comment #14
tim.plunkettCustomBlockDeleteForm::buildForm() still has request in it.
Weird extra line here
Use ['#title'] please
Comment #15
benjy commentedFixed #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?Comment #16
benjy commentedNever mind, just realised the @todo was relative to drupal_set_title() new patch and interdiff attached. Just ignore #15
Comment #17
tim.plunkettEntityFormControllerNG already implements ContainerInjectionInterface, you can remove the ControllerInterface bit
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.
Missing space after if
Comment #18
benjy commented1. 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.
Comment #20
jibranlanguageManager declaration is missing.
Comment #21
benjy commentedFixed.
Comment #22
benjy commentedStraight reroll.
Comment #23
tim.plunkettUnneened space
The parent::buildForm needs to be at the end of the method.
Comment #24
benjy commentedNew patch attached.
Comment #25
benjy commentedMoved buildForm() to bottom of method. Interdiff is with #22
Comment #27
benjy commentedLet's try that again.
Comment #29
benjy commentedComment #30
dawehnerHey, I thought we talk about custom blocks :)
Comment #31
benjy commentedThanks.
Comment #32
dawehnerInterdiff looks fine!
Comment #33
alexpottNot used
form_set_error is deprecated - should use the form builder service.
Now you can remove the
use Symfony\Component\HttpFoundation\Request;from the top of the CustomBlockDeleteForm.php fileuser_access()which is also deprecated and can be replaced with$this->currentUser()->hasPermission()Comment #34
benjy commentedThanks, feedback done.
Comment #36
alexpott@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!
Comment #37
benjy commented@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.
Comment #40
benjy commented31: customblockformcontroller-2060865-31.patch queued for re-testing.
Comment #42
benjy commentedNew 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.
Comment #43
olli commentedCan we use $this->url() here?
This @todo is for this issue.
Comment #44
benjy commentedGood catches.
Comment #45
tim.plunkettFormBase::url() takes a route name, if this doesn't fail its a bad thing. Should be
$this->url('block.admin_display')Comment #47
benjy commentedThanks Tim.
Comment #48
larowlanThis can use FormBuilder::setErrorByName() now, which can be injected
Comment #49
benjy commented@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.
Comment #50
larowlanIn that case
Next time I'll read all the comments
Comment #51
benjy commentedRe-rolled.
Comment #54
benjy commentedLooks like how we get the entity manager changed.
Comment #56
benjy commented54: modernize-custom-block-2060865-54.patch queued for re-testing.
Comment #57
dawehnerform_set_error can now be replaced by \Drupal::formBuilder()->setErrorByName($name, $form_state, $message);
Comment #58
benjy commented@dawehner formBuilder has come up a few times in this thread. We took #36 as consensus?
Comment #59
tim.plunkettOnce #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()
Comment #60
benjy commentedBack 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.
Comment #61
larowlanback again
Comment #62
tim.plunkettPatch no longer applied because #2145007: Convert form_set_error() in FormBase classes to use FormErrorInterface changed form_set_error :)
Straight reroll.
Comment #63
webchick62: custom_block-2060865-62.patch queued for re-testing.
Comment #65
larowlanre-roll
Comment #66
andypostLooks RTBC except one nitpick
Suppose better:
if ($this..('language') && $block->isNew()) {
...
}
Comment #67
benjy commented$language_configuration is used again further down in the form so changing that if statement would change how it works.
Comment #68
xjm65: cb-modern.66.patch queued for re-testing.
Comment #69
alexpott$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.
Comment #70
benjy commentedFixed.
Comment #71
larowlanComment #72
webchickCommitted and pushed to 8.x. Thanks!