class EntityBundle

Same name in this branch
  1. 4.0.x src/Plugin/Condition/EntityBundle.php \Drupal\ctools\Plugin\Condition\EntityBundle
Same name in other branches
  1. 8.x-3.x src/Plugin/Deriver/EntityBundle.php \Drupal\ctools\Plugin\Deriver\EntityBundle
  2. 8.x-3.x src/Plugin/Condition/EntityBundle.php \Drupal\ctools\Plugin\Condition\EntityBundle

Deriver that creates a condition for each entity type with bundles.

Hierarchy

Expanded class hierarchy of EntityBundle

File

src/Plugin/Deriver/EntityBundle.php, line 10

Namespace

Drupal\ctools\Plugin\Deriver
View source
class EntityBundle extends EntityDeriverBase {
    
    /**
     * {@inheritdoc}
     */
    public function getDerivativeDefinitions($base_plugin_definition) {
        // Do not define any derivatives on Drupal 9.3+, instead, replace the core
        // class in ctools_condition_info_alter().
        if (\version_compare(\Drupal::VERSION, '9.3', '>')) {
            return [];
        }
        foreach ($this->entityTypeManager
            ->getDefinitions() as $entity_type_id => $entity_type) {
            if ($entity_type->hasKey('bundle')) {
                $this->derivatives[$entity_type_id] = $base_plugin_definition;
                $this->derivatives[$entity_type_id]['label'] = $this->getEntityBundleLabel($entity_type);
                $this->derivatives[$entity_type_id]['context_definitions'] = [
                    "{$entity_type_id}" => new EntityContextDefinition('entity:' . $entity_type_id),
                ];
            }
        }
        return $this->derivatives;
    }
    
    /**
     * Provides the bundle label with a fallback when not defined.
     *
     * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
     *   The entity type we are looking the bundle label for.
     *
     * @return \Drupal\Core\StringTranslation\TranslatableMarkup
     *   The entity bundle label or a fallback label.
     */
    protected function getEntityBundleLabel($entity_type) {
        if ($label = $entity_type->getBundleLabel()) {
            return $this->t('@label', [
                '@label' => $label,
            ]);
        }
        $fallback = $entity_type->getLabel();
        if ($bundle_entity_type = $entity_type->getBundleEntityType()) {
            // This is a better fallback.
            $fallback = $this->entityTypeManager
                ->getDefinition($bundle_entity_type)
                ->getLabel();
        }
        return $this->t('@label bundle', [
            '@label' => $fallback,
        ]);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
EntityBundle::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
EntityBundle::getEntityBundleLabel protected function Provides the bundle label with a fallback when not defined.
EntityDeriverBase::$entityFieldManager protected property The entity field manager.
EntityDeriverBase::$entityTypeManager protected property The entity type manager.
EntityDeriverBase::$entityTypeRepository protected property The entity type repository.
EntityDeriverBase::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
EntityDeriverBase::__construct public function Constructs new EntityViewDeriver.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.