Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Create helper methods for creating the add buttons, such as buildAddButton() / buildAddButtonSelect(). We need to find a way to put the spaghetti more into component logic
Proposed resolution
Create helper methods, reduce as much as possible the code in InlineParagraphsWidget.
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#31 | interdiff-2830550-29-31.txt | 1.16 KB | toncic |
#31 | create_helper_methods-2830550-31.patch | 15.53 KB | toncic |
#29 | interdiff-2830550-26-29.txt | 7.42 KB | toncic |
#29 | create_helper_methods-2830550-29.patch | 15.55 KB | toncic |
#26 | interdiff-2830550-24-26.txt | 3.6 KB | toncic |
Comments
Comment #2
johnchqueI think that would remove some code, and seeing the InlineParagraphsWidget file, there is a lot of code that can be removed. I don't see any user case where this select mode is really needed, maybe for touch screens?
Comment #3
miro_dietikerLet's see how much code we can drop if we remove this.
I'm highly open to user inputs if this is really important to someone. But yeah, code complexity sucks to maintain 4 different ways of adding paragraphs... especially if we add the widget soon at #2236905: [META] Nicer UI / Icons for "Paragraph type" + "Add another Paragraph"
Comment #4
johnchqueActually the reduced code is not that many.
Comment #7
miro_dietikerYeah, and the tests...
But for the moment that's not making a big difference, thus cancelling this experiment.
What we should do though, is outsource these things into helper methods, say buildAddButton() / buildAddButtonSelect().
We also had the idea to build helper methods for the widget that are based on the widget/edit mode.
This is an important step since we need to find a way to put the spaghetti more into component logic, since our master plan will add even more modes and logic.
Comment #8
johnchqueOk, let's change this issue to do that then.
Comment #9
toncic CreditAttribution: toncic at MD Systems GmbH commentedI will try this one.
Comment #10
toncic CreditAttribution: toncic at MD Systems GmbH commentedExtract code for creating add_more button into new buildAddButtonSelect method.
Comment #11
miro_dietikerSome quick reply.
That reads much unfortunate... I think we need a more specific battleplan about how we can outsource this, and if at all.
Should we put more to the form state and work with these elements? Or use some other helpers to get these values?
Not sure of we are allowed to put anything into a member variable?
Please don't move this method.
Comment #12
BerdirThis can be improved a lot by moving some more code into the helper method and changing some things so that we don't insert into $elements anymore but just return the inner element. Means we need to keep the primary if in there or we need to handle returning nothing. I think that's OK like that.
Some more refactoring would be possible now, e.g. using early returns for the special cases instead of many nested if/else conditions.
$items and $form_state can be avoided by moving the translation init out of the helper and making it part of the if condition instead of using #access.
Not sure about $parents, $id_prefix, $wrapper_id. Could use object properties to pass them around but the amount of variables is probably acceptable now.
Similar for $real_items_count, that is fun anyway, if I see that correctly, we we drop paragraphs that you don't have access to when editing? seems like we'd need some kind of read-only mode there and then this might also become simpler.
Also needs documentation and maybe a bette rmethod name, I just took the existing one.
Comment #13
toncic CreditAttribution: toncic at MD Systems GmbH commentedMade two new methods
buildDropDown
andbuildList
. Also extract some parameters as class property. I think that we need to rename new properties to something similar because we have local variables in formElement, and that could be very confusing.Comment #14
toncic CreditAttribution: toncic at MD Systems GmbH commentedAdded one new helper method getAccessOptions(). Only need to figure out how to remove add_more_elements from parameters.
Also changed syntax for array.
Comment #15
toncic CreditAttribution: toncic at MD Systems GmbH commentedNow is looking fine, ready for review.
Comment #16
johnchqueLet's change this to buildAddActions
this is much simpler if you just do if(count($this->getAllowedTypes))
Wrong logic, should be inverted. If there are no allowedTypes the message should be You are not allowed to add any of the...
This if is kind of odd, let's use a case here:
case 'button':
$this->buildButtonsAddMode();
break;
case 'dropdown':
$this->buildButtonsAddMode(TRUE);
break;
case 'select':
default:
$this->buildSelectAddMode();
break;
$entity_type_manager
This actually build two add modes, so let's call it buildButtonsAddMode and send an optional argument in there TRUE or FALSE to make it build the dropdown if TRUE.
If we implement the previous comment then this if can just check if ($dropdown) because we are checking the accessOptions above.
This is not the list, let's change it to buildSelectAddMode()
Comment #17
miro_dietikerSome quick responses for improvements. :-)
Cross-post, but don't have time to cross-check with john, sorry
just
return $this->buildList()
Nothing more below.
OK the previous variable name was access_options, but we should seek a better name for a method.
getAllowedTypesWithAccess() or so.
That's strange. Just because those dragdrop_settings are there, it's a pass.
Instead, we should check the drag_drop settings, if the type is enabled.
The settings would otherwise be dysfunctional.
However, this logic doesn't change, so let's cover this problem in a follow-up and investigate more!
What DropDown?
What list?
Comment #18
miro_dietikerI think one setWidgetState call would be enough?
Comment #19
toncic CreditAttribution: toncic at MD Systems GmbH commented@yongt9412 :
#16.4 @Berdit said that can stay like this for now.
#16.6 I don't think we need boolean argument here. That will not change logic a lot and I think is more readable, because the name of function is not clearly saying what should be parameter.
@miro_dietiker
#18 I think that we can not use only one, test are failing.
Created follow-up: #2834362: Change logic in getAllowedTypesWithAccess
Comment #20
johnchqueMode != Model
Comment #21
toncic CreditAttribution: toncic at MD Systems GmbH commentedMode instead of Model.
Comment #24
toncic CreditAttribution: toncic at MD Systems GmbH commented@yongt9412 :
As I was thought, we can not use
if (count($this->getAllowedTypes())) {
instead ofif (count($this->getAllowedTypes()) == 0) {
.Comment #25
miro_dietikerBoth lines inverted the logic.
Comment #26
toncic CreditAttribution: toncic at MD Systems GmbH commented@miro_dietiker yes, I did that after discussing with @Berdir. We are checking at the beginning of function and in that way we avoid else statement.
I forgot to switch other properties to be camelCase.
Comment #27
BerdirThose descriptions don't really tell me anything. also id should be ID
int, not integer
why no field prefix here?
Available => Accessible?
the two set/get calls to this were there before, we're just renaming variables. I think changing that would be better be done in a separate issue.
I think we should also open an issue to investigate this real item count stuff together with access.
I find that highly confusing, as I said before, If there are 3 texts and a text + item (which I do not have access to) on a field that allows 4 values then the one I don't have access to will be removed and I will be allowed to add a nother one?
IMHO, we need an access_denied state, that displays a message that you can't edit a paragraph. Similar to closed, but can't be opened.
But that's a separate issue.
Not sure why we change this from #type container with classes to markup. Also we're not closing the em?
spaces before ;
Also not sure if I like not having an explicit else here. While it is not technically required I think it would help to increase readability. And we might add more option later on.
The naming here is IMHO a bit strange, the method is getAllowedTypes() but the propertiy is accessOptions. If it is options then this should maybe be called getAccessibleOptions()
Also documentation says *type* (singluar) and should clarify that it returns options the user has access to.
we already check this outside, this access can go.
Comment #28
BerdirComment #29
toncic CreditAttribution: toncic at MD Systems GmbH commentedImplemented comment #27.
Created two new issues: #2834824: Two setWidgetState calls in formElementMultiple and #2834829: Add tests for per-type access checking.
And also if we delete
'#access' => !$this->isTranslating,
test are failing.Comment #30
BerdirThe #access is needed because I added an $this->isTranslating check directly to this, so we never even went inside. Looks like this was removed again, is there a reason for that?
Comment #31
toncic CreditAttribution: toncic at MD Systems GmbH commentedImplemented comment #30.
Comment #32
johnchqueI think we don't need the getAccessibleOptions check here since before in the buildAddActions we are already checking if the accessible options are not 0.
Comment #33
BerdirThat code confused me as well for a second, but we are looking for *more than one* there, not at least one.
Comment #36
BerdirI guess this is ready then.
Comment #38
miro_dietikerAlmost committed, but those methods should be protected...
Anyway... decided to do dirty quickfix and commit it with on-the-fly changingn to protected... Crozz fingerz for tests to pass! :-)