Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fragment priority when reusing the same identifier #6471

Open
m-vo opened this issue Oct 27, 2023 · 4 comments · May be fixed by #7012
Open

Fragment priority when reusing the same identifier #6471

m-vo opened this issue Oct 27, 2023 · 4 comments · May be fixed by #7012
Assignees
Labels
Milestone

Comments

@m-vo
Copy link
Member

m-vo commented Oct 27, 2023

Affected version(s)

5.3

Description

Currently, there are two issues when redeclaring a content element/frontend module type.

If you want to replace the text content element, you could for instance do something like this:

// App/Controller/ContentElement/MyTextController.php
#[AsContentElement(type: 'text', category: 'texts', template: 'content_element/text')]
class MyTextController extends AbstractContentElement { 
  // […]
}

But the core's text type would still be used. Additionally the debug:fragments command would output two entries for text but both would show the core's class.

The reason for this is, that the RegisterFragmentPass uses priority tagged services, i.e. you need to manually tag your service with a priority (it does not work in the attribute). I would argue, that we should probably use the bundle loading order instead or - at least - the app should always come last by default if not configured differently.

The issue with debug:fragments is something else: there the add() method assumes it is only called once per identifier. We should probably store all information together (data[] = […, …, …]) instead of in three different arrays.

As this whole setup is compile time only, we could also change the logic in the RegisterFragmentPass to only add the effective calls to the definitions once all found entities were processed.

/cc @bytehead

@bytehead bytehead self-assigned this Oct 27, 2023
@leofeyer leofeyer added this to the 4.13 milestone Oct 27, 2023
@aschempp
Copy link
Member

I always assumed the order of bundles affects the sorting as well as the priority, simply because the array that stored the information must have an order. However, due to sorting algorithms, that could actually be lost.

Regarding the command list, would you still get two text elements when priority is used? That looks like a bug to me.

@m-vo
Copy link
Member Author

m-vo commented Oct 27, 2023

Regarding the command list, would you still get two text elements when priority is used? That looks like a bug to me.

Yes, because there are still two independent services. And for each of them we add calls for the fragment registry and the debug command. If there are two services with the same type, we should IMHO either only add method calls for the latter (which would solve the problem in the debug command) or keep the behavior but then store the data in the command differently or index it by identifier, so that only the effective one is shown.

After all, in the the registry, there is a comment suggesting, that it's fine to add multiple calls with the same identifier:

public function add(string $identifier, FragmentConfig $config): FragmentRegistryInterface
{
// Override existing fragments with the same identifier
$this->fragments[$identifier] = $config;
return $this;
}

@m-vo
Copy link
Member Author

m-vo commented Oct 27, 2023

By the way, currently the debug command would just output identical rows, because it iterates the identifiers but looks up the wrong data. 😄

@bytehead bytehead modified the milestones: 4.13, 5.3 Mar 12, 2024
@bytehead
Copy link
Member

The problem only occurs in 5.x

@bytehead bytehead linked a pull request Mar 12, 2024 that will close this issue
@bytehead bytehead linked a pull request Mar 12, 2024 that will close this issue
@bytehead bytehead linked a pull request Mar 12, 2024 that will close this issue
leofeyer pushed a commit that referenced this issue Mar 20, 2024
Description
-----------

Preparation PR to fix the issue #6471 afterwards.

Commits
-------

9e49973 Use dedicated fragment registry for debug:fragments command
13dc311 Adjust tests
afd98f8 Refactor
a5d730f Update core-bundle/src/Command/DebugFragmentsCommand.php
fb1f24a Loop directly iterate over keys and values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants