The naming of Drupal\ctools\Plugin\Condition\EntityType makes no sense, it also still has 'Node Type' in some of the comments.

It should be called EntityBundle or maybe just Bundle. It's about checking the bundle of an entity and not the entity type.

Additionally, the deriver shouldn't be checking the bundle entity types but instead look for a 'bundle' entity key and use getBundleLabel(). bundle_of/bundle_entity_type are optional, bundles usually are but must not be entity types.

Major because this prevents me from fixing the userpoints tests.

Attaching a minimal patch that adds the config schema but I really think this should be renamed and cleaned up before people start using it.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir created an issue. See original summary.

EclipseGc’s picture

Status: Needs review » Fixed

committed.

  • EclipseGc committed 1a3807e on 8.x-3.x authored by Berdir
    Issue #2615138 by Berdir: The generic EntityType condition has a very...
Berdir’s picture

Status: Fixed » Active

Thanks, keeping this issue open for now, I'l try to provide a patch to rename things.

Status: Active » Needs work

The last submitted patch, entity-type-schema.patch, failed testing.

Berdir’s picture

Status: Needs work » Needs review
FileSize
7.41 KB

Here's a patch, some explanation in next ocmment.

Berdir’s picture

  1. +++ b/src/Plugin/Condition/EntityBundle.php
    @@ -57,11 +54,10 @@ class EntityType extends ConditionPluginBase implements ConstraintConditionInter
    -  public function __construct(EntityManagerInterface $entity_manager, array $configuration, $plugin_id, $plugin_definition) {
    +  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, array $configuration, $plugin_id, $plugin_definition) {
         parent::__construct($configuration, $plugin_id, $plugin_definition);
    -    $this->entityStorage = $entity_manager->getStorage($this->getDerivativeId());
    -    $this->bundleType = $entity_manager->getDefinition($this->getDerivativeId());
    -    $this->bundleOf = $entity_manager->getDefinition($this->bundleType->getBundleOf());
    +    $this->entityTypeBundleInfo = $entity_type_bundle_info;
    +    $this->bundleOf = $entity_type_manager->getDefinition($this->getDerivativeId());
    

    Using the new services. Also bundleType is no longer needed.

  2. +++ b/src/Plugin/Condition/EntityBundle.php
    @@ -122,10 +119,10 @@ class EntityType extends ConditionPluginBase implements ConstraintConditionInter
           $bundles = implode(', ', $bundles);
    -      return $this->t('The @entity_type @bundle_type is @bundles or @last', array('@entity_type' => strtolower($this->bundleOf->getLabel()), '@bundle_type' => strtolower($this->bundleType->getLabel()), '@bundles' => $bundles, '@last' => $last));
    +      return $this->t('@bundle_type is @bundles or @last', array('@bundle_type' => $this->bundleOf->getBundleLabel(), '@bundles' => $bundles, '@last' => $last));
         }
         $bundle = reset($this->configuration['bundles']);
    -    return $this->t('The @entity_type @bundle_type is @bundle', array('@entity_type' => strtolower($this->bundleOf->getLabel()), '@bundle_type' => strtolower($this->bundleType->getLabel()), '@bundle' => $bundle));
    +    return $this->t('@bundle_type is @bundle', array('@bundle_type' => $this->bundleOf->getBundleLabel(), '@bundle' => $bundle));
       }
     
       /**
    

    This part is tricky. Two problems:

    a) "@entity_type @bundle_type" IMHO leads to weird double names: "content content type" or "custom block custom block type". So I think just the bundle label is enough.

    b) lowercasing labels is problematic because that is only correct for english, in german, they have to stay uppercase. There are some pending issues around that. for the label, there's a special method but that actually has the same problem. it doesn't exist for bundle label, though. So I tried to leave out the The so that it can be at the beginning of the sentence. Not really sure about tha, maybe it could also be changed to not be a sentence but just "Node type: A, B or C" ?

  3. +++ b/src/Plugin/Deriver/EntityBundle.php
    @@ -1,27 +1,28 @@
       public function getDerivativeDefinitions($base_plugin_definition) {
         foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
    -      $bundle_of = $entity_type->getBundleOf();
    -      if ($bundle_of) {
    +      if ($entity_type->hasKey('bundle')) {
             $this->derivatives[$entity_type_id] = $base_plugin_definition;
    -        $this->derivatives[$entity_type_id]['label'] = $this->t('@label', ['@label' => $entity_type->getLabel()]);
    

    This changes the plugin ID from entity_bundle:node, so the bundle of node, which I think makes a lot more sense than entity_type:node_type, which sounds like it checks that the entity type is node_type?

EclipseGc’s picture

Status: Needs review » Fixed

Looks good!

Fixed

Eclipse

  • EclipseGc committed d219d71 on 8.x-3.x authored by Berdir
    Issue #2615138 by Berdir: The generic EntityType condition has a very...

Status: Fixed » Closed (fixed)

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