Problem/Motivation

Some parts of EntityReferenceController are calling global functions instead of using injection etc.
Just a clean up.

Proposed resolution

Clean up

Remaining tasks

Review

User interface changes

None

API changes

None

Comments

larowlan’s picture

Status: Active » Needs review
tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

Sure, this is harmless, and swaps more functions for methods.

amateescu’s picture

+++ b/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceController.php
@@ -31,9 +33,12 @@ class EntityReferenceController extends ControllerBase {
+    $this->entityManager = $entity_manager;

Since ControllerBase provides $this->entityManager() now, shouldn't we remove the container get call from our create() method instead?

Status: Reviewed & tested by the community » Needs work

The last submitted patch, entity-ref-autocomplete.1.patch, failed testing.

larowlan’s picture

Status: Needs work » Needs review
StatusFileSize
new3.81 KB

Reroll and fixes #3

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Looks good now, thanks :)

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 5: entity-ref-autocomplete.2.patch, failed testing.

mikemiles86’s picture

Assigned: Unassigned » mikemiles86

Last patch failed due to changes to PSR-4. Re-rolling patch.

mikemiles86’s picture

Assigned: mikemiles86 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new3.63 KB

re-rolled patch.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new2.68 KB
+++ b/core/modules/entity_reference/src/EntityReferenceAutocomplete.php
@@ -88,8 +89,11 @@ public function getMatches(FieldDefinitionInterface $field_definition, $entity_t
-      $widget = entity_get_form_display($entity_type, $bundle, 'default')->getComponent($field_definition->getName());
...
+      if ($display = $this->entityManager->getStorage('entity_form_display')->load($entity_type . '.' . $bundle . '.default')) {

entity_get_form_display() does a bit more than just loading the entity display. Rerolled without that part and the rest looks good.

berdir’s picture

@amateescu: entity_get_form_display() and entity_get_display() are one of the few functions left in entity.inc that do not have a OOP replacement. Should we open an issue to add one? Not sure if it will be accepted in 8.0.x, but we can try. Any thoughts on where to put it? static method on the entity classes similar to loadByName()?

Any opinions on the name? EntityViewDisplay::getDisplay() ? or more explicit on what it does like loadOrCreate()?

amateescu’s picture

Yep, having those as static methods "somewhere" sounds good. However, if we put them in their respective entity classes might make them a bit less discoverable, so maybe STUFF ALL THE THINGS inside EntityManager? :)

EntityManager::getViewDisplay() and ::getFormDisplay() is what I'd propose.

Not sure if it will be accepted in 8.0.x,

It should be if we leave the functions in place as a wrapper.

berdir’s picture

That works for me too, although there's an issue open to split up the entity manager into an entity field manager or something, as it's getting huge.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/entity_reference/src/EntityReferenceAutocomplete.php
    @@ -98,7 +99,7 @@ public function getMatches(FieldDefinitionInterface $field_definition, $entity_t
    -          $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
    +          $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(String::decodeEntities(strip_tags($key)))));
    
    +++ b/core/modules/entity_reference/src/EntityReferenceController.php
    @@ -86,7 +87,7 @@ public function handleAutocomplete(Request $request, $type, $field_name, $entity
    -    $last_item = drupal_strtolower(array_pop($items_typed));
    +    $last_item = Unicode::strtolower(array_pop($items_typed));
    

    These changes are out of scope since deprecated functions have other issues to do this.

  2. +++ b/core/modules/entity_reference/src/EntityReferenceController.php
    @@ -8,7 +8,9 @@
    +use Drupal\Core\Entity\EntityManagerInterface;
    

    How come?

  3. +++ b/core/modules/entity_reference/src/EntityReferenceController.php
    @@ -41,8 +43,7 @@ public function __construct(EntityReferenceAutocomplete $entity_reference_autoco
    -      $container->get('entity_reference.autocomplete'),
    -      $container->get('entity.manager')
    +      $container->get('entity_reference.autocomplete')
    

    This is fixing a bug. Let's rescope this issue to do just this.

amateescu’s picture

Title: Cleanup EntityReferenceController to avoid some global procedural calls » Cleanup EntityReferenceController's create() method
Category: Task » Bug report
Priority: Minor » Normal
Status: Needs work » Needs review
StatusFileSize
new678 bytes

Okey dokey.

amateescu’s picture

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Hard to find a patch simpler than this one.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

This issue is a normal bug fix, and doesn't include any disruptive changes, so it is allowed per #2350615: [policy, no patch] What changes can be accepted during the Drupal 8 beta phase?. Committed de1d788 and pushed to 8.0.x. Thanks!

  • alexpott committed de1d788 on 8.0.x
    Issue #2268753 by amateescu, larowlan, mikemiles86: Fixed Cleanup...

Status: Fixed » Closed (fixed)

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