Loading core/lib/Drupal/Core/Config/Action/Attribute/ActionMethod.php +13 −0 Original line number Diff line number Diff line Loading @@ -5,7 +5,9 @@ namespace Drupal\Core\Config\Action\Attribute; // cspell:ignore inflector use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; use Drupal\Core\Config\Action\Exists; use Drupal\Core\Extension\ExtensionDiscovery; use Drupal\Core\StringTranslation\TranslatableMarkup; /** Loading @@ -30,12 +32,23 @@ final class ActionMethod { * ID. For example, if the method is called 'addArray' this can be set to * 'addMultipleArrays'. Set to FALSE if a pluralized version does not make * logical sense. * @param string|null $name * The name of the action, if it should differ from the method name. Will be * pluralized if $pluralize is TRUE. Must follow the rules for a valid PHP * function name (e.g., no spaces, no Unicode characters, etc.). If used, * the actual name of the method will NOT be available as an action name. * * @see https://www.php.net/manual/en/functions.user-defined.php */ public function __construct( public readonly Exists $exists = Exists::ErrorIfNotExists, public readonly TranslatableMarkup|string $adminLabel = '', public readonly bool|string $pluralize = TRUE, public readonly ?string $name = NULL, ) { if ($name && !preg_match(ExtensionDiscovery::PHP_FUNCTION_PATTERN, $name)) { throw new InvalidPluginDefinitionException('entity_method', sprintf("'%s' is not a valid PHP function name.", $name)); } } } core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/Deriver/EntityMethodDeriver.php +3 −2 Original line number Diff line number Diff line Loading @@ -102,15 +102,16 @@ private function processMethod(\ReflectionMethod $method, ActionMethod $action_a 'pluralized' => FALSE, ]; $derivative['entity_types'] = [$entity_type->id()]; $action_name = $action_attribute->name ?: $method->name; // Build a config action identifier from the entity type's config // prefix and the method name. For example, the Role entity adds a // 'user.role:grantPermission' action. $this->addDerivative($method->name, $entity_type, $derivative, $method->name); $this->addDerivative($action_name, $entity_type, $derivative, $method->name); $pluralized_name = match(TRUE) { is_string($action_attribute->pluralize) => $action_attribute->pluralize, $action_attribute->pluralize === FALSE => '', default => $this->inflector->pluralize($method->name)[0] default => $this->inflector->pluralize($action_name)[0] }; // Add a pluralized version of the plugin. if (strlen($pluralized_name) > 0) { Loading core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +3 −0 Original line number Diff line number Diff line Loading @@ -160,6 +160,7 @@ public function get($property_name) { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Set a value'), pluralize: 'setMultiple')] public function set($property_name, $value) { if ($this instanceof EntityWithPluginCollectionInterface && !$this->isSyncing()) { $plugin_collections = $this->getPluginCollections(); Loading @@ -177,6 +178,7 @@ public function set($property_name, $value) { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Enable'), pluralize: FALSE)] public function enable() { return $this->setStatus(TRUE); } Loading @@ -184,6 +186,7 @@ public function enable() { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Disable'), pluralize: FALSE)] public function disable() { return $this->setStatus(FALSE); } Loading core/lib/Drupal/Core/Entity/EntityDisplayBase.php +1 −0 Original line number Diff line number Diff line Loading @@ -373,6 +373,7 @@ public function setComponent($name, array $options = []) { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Hide component'), name: 'hideComponent')] public function removeComponent($name) { $this->hidden[$name] = TRUE; unset($this->content[$name]); Loading core/lib/Drupal/Core/Field/FieldConfigBase.php +6 −1 Original line number Diff line number Diff line Loading @@ -329,7 +329,7 @@ public function getLabel() { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Change field label'))] #[ActionMethod(adminLabel: new TranslatableMarkup('Set field label'), pluralize: FALSE)] public function setLabel($label) { $this->label = $label; return $this; Loading @@ -345,6 +345,7 @@ public function getDescription() { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Set field description'), pluralize: FALSE)] public function setDescription($description) { $this->description = $description; return $this; Loading @@ -361,6 +362,7 @@ public function isTranslatable() { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Set whether field is translatable'), pluralize: FALSE)] public function setTranslatable($translatable) { $this->translatable = $translatable; return $this; Loading @@ -376,6 +378,7 @@ public function getSettings() { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Set field settings'), pluralize: FALSE)] public function setSettings(array $settings) { $this->settings = $settings + $this->settings; return $this; Loading Loading @@ -411,6 +414,7 @@ public function isRequired() { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Set whether field is required'), pluralize: FALSE)] public function setRequired($required) { $this->required = $required; return $this; Loading Loading @@ -443,6 +447,7 @@ public function getDefaultValueLiteral() { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Set default value'), pluralize: FALSE)] public function setDefaultValue($value) { $this->default_value = $this->normalizeValue($value, $this->getFieldStorageDefinition()->getMainPropertyName()); return $this; Loading Loading
core/lib/Drupal/Core/Config/Action/Attribute/ActionMethod.php +13 −0 Original line number Diff line number Diff line Loading @@ -5,7 +5,9 @@ namespace Drupal\Core\Config\Action\Attribute; // cspell:ignore inflector use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; use Drupal\Core\Config\Action\Exists; use Drupal\Core\Extension\ExtensionDiscovery; use Drupal\Core\StringTranslation\TranslatableMarkup; /** Loading @@ -30,12 +32,23 @@ final class ActionMethod { * ID. For example, if the method is called 'addArray' this can be set to * 'addMultipleArrays'. Set to FALSE if a pluralized version does not make * logical sense. * @param string|null $name * The name of the action, if it should differ from the method name. Will be * pluralized if $pluralize is TRUE. Must follow the rules for a valid PHP * function name (e.g., no spaces, no Unicode characters, etc.). If used, * the actual name of the method will NOT be available as an action name. * * @see https://www.php.net/manual/en/functions.user-defined.php */ public function __construct( public readonly Exists $exists = Exists::ErrorIfNotExists, public readonly TranslatableMarkup|string $adminLabel = '', public readonly bool|string $pluralize = TRUE, public readonly ?string $name = NULL, ) { if ($name && !preg_match(ExtensionDiscovery::PHP_FUNCTION_PATTERN, $name)) { throw new InvalidPluginDefinitionException('entity_method', sprintf("'%s' is not a valid PHP function name.", $name)); } } }
core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/Deriver/EntityMethodDeriver.php +3 −2 Original line number Diff line number Diff line Loading @@ -102,15 +102,16 @@ private function processMethod(\ReflectionMethod $method, ActionMethod $action_a 'pluralized' => FALSE, ]; $derivative['entity_types'] = [$entity_type->id()]; $action_name = $action_attribute->name ?: $method->name; // Build a config action identifier from the entity type's config // prefix and the method name. For example, the Role entity adds a // 'user.role:grantPermission' action. $this->addDerivative($method->name, $entity_type, $derivative, $method->name); $this->addDerivative($action_name, $entity_type, $derivative, $method->name); $pluralized_name = match(TRUE) { is_string($action_attribute->pluralize) => $action_attribute->pluralize, $action_attribute->pluralize === FALSE => '', default => $this->inflector->pluralize($method->name)[0] default => $this->inflector->pluralize($action_name)[0] }; // Add a pluralized version of the plugin. if (strlen($pluralized_name) > 0) { Loading
core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +3 −0 Original line number Diff line number Diff line Loading @@ -160,6 +160,7 @@ public function get($property_name) { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Set a value'), pluralize: 'setMultiple')] public function set($property_name, $value) { if ($this instanceof EntityWithPluginCollectionInterface && !$this->isSyncing()) { $plugin_collections = $this->getPluginCollections(); Loading @@ -177,6 +178,7 @@ public function set($property_name, $value) { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Enable'), pluralize: FALSE)] public function enable() { return $this->setStatus(TRUE); } Loading @@ -184,6 +186,7 @@ public function enable() { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Disable'), pluralize: FALSE)] public function disable() { return $this->setStatus(FALSE); } Loading
core/lib/Drupal/Core/Entity/EntityDisplayBase.php +1 −0 Original line number Diff line number Diff line Loading @@ -373,6 +373,7 @@ public function setComponent($name, array $options = []) { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Hide component'), name: 'hideComponent')] public function removeComponent($name) { $this->hidden[$name] = TRUE; unset($this->content[$name]); Loading
core/lib/Drupal/Core/Field/FieldConfigBase.php +6 −1 Original line number Diff line number Diff line Loading @@ -329,7 +329,7 @@ public function getLabel() { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Change field label'))] #[ActionMethod(adminLabel: new TranslatableMarkup('Set field label'), pluralize: FALSE)] public function setLabel($label) { $this->label = $label; return $this; Loading @@ -345,6 +345,7 @@ public function getDescription() { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Set field description'), pluralize: FALSE)] public function setDescription($description) { $this->description = $description; return $this; Loading @@ -361,6 +362,7 @@ public function isTranslatable() { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Set whether field is translatable'), pluralize: FALSE)] public function setTranslatable($translatable) { $this->translatable = $translatable; return $this; Loading @@ -376,6 +378,7 @@ public function getSettings() { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Set field settings'), pluralize: FALSE)] public function setSettings(array $settings) { $this->settings = $settings + $this->settings; return $this; Loading Loading @@ -411,6 +414,7 @@ public function isRequired() { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Set whether field is required'), pluralize: FALSE)] public function setRequired($required) { $this->required = $required; return $this; Loading Loading @@ -443,6 +447,7 @@ public function getDefaultValueLiteral() { /** * {@inheritdoc} */ #[ActionMethod(adminLabel: new TranslatableMarkup('Set default value'), pluralize: FALSE)] public function setDefaultValue($value) { $this->default_value = $this->normalizeValue($value, $this->getFieldStorageDefinition()->getMainPropertyName()); return $this; Loading