Problem/Motivation
The logic in ConfigManager::getEntityTypeIdByName() is needlessly complicated:
public function getEntityTypeIdByName($name) {
$entities = array_filter($this->entityTypeManager->getDefinitions(), function (EntityTypeInterface $entity_type) use ($name) {
return ($entity_type instanceof ConfigEntityTypeInterface && $config_prefix = $entity_type->getConfigPrefix()) && strpos($name, $config_prefix . '.') === 0;
});
return key($entities);
}
It's using array_filter with a custom filter callback to find those entity types whose config prefix matches the given config $name.
But it then returns only the first one found, using key() -- since there should only be one.
I think it would be much easier to read and debug if it did this:
foreach (($this->entityTypeManager->getDefinitions() as $entity_type) {
if (condition ... ) {
return entity type ID
}
}
Steps to reproduce
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Issue fork drupal-3328066
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #4
rpayanmComment #5
joachim commentedComment #6
rpayanm@joachim Thank you for reviewing it, I created a new commit.
Comment #7
joachim commentedLooks good. Thanks!
Comment #10
xjmNice code cleanup! Thanks @joachim for finding it and @rpayanm for implementing a nice clean fix.
Committed to 10.1.x and cherry-picked to 10.0.x. Since it uses
str_starts_with(), we can't backport it to 9.5.x, but I think that's fine. There would be a merge conflict on the line either way if we usedstrpos(). (See also #3328456: Replace substr($a, 0, $i) with str_starts_with() and #3328443: Replace most strpos() === 0 or !== 0 with str_starts_with().)