Verified Commit d9a1ec2d authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3303127 by phenaproxima, thejimbirch, alexpott, Wim Leers, nedjo,...

Issue #3303127 by phenaproxima, thejimbirch, alexpott, Wim Leers, nedjo, b_sharpe, laura.j.johnson@gmail.com: Determine which core config entity methods should be config actions
parent 269e502c
Loading
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -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;

/**
@@ -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));
    }
  }

}
+3 −2
Original line number Diff line number Diff line
@@ -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) {
+3 −0
Original line number Diff line number Diff line
@@ -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();
@@ -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);
  }
@@ -184,6 +186,7 @@ public function enable() {
  /**
   * {@inheritdoc}
   */
  #[ActionMethod(adminLabel: new TranslatableMarkup('Disable'), pluralize: FALSE)]
  public function disable() {
    return $this->setStatus(FALSE);
  }
+1 −0
Original line number Diff line number Diff line
@@ -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]);
+6 −1
Original line number Diff line number Diff line
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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