function EntityMethod::applySingle

Same name in other branches
  1. 10 core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/EntityMethod.php \Drupal\Core\Config\Action\Plugin\ConfigAction\EntityMethod::applySingle()

Applies the action to entity treating the $values array a single call.

Parameters

\Drupal\Core\Config\Entity\ConfigEntityInterface $entity: The entity to apply the action to.

mixed $value: The value for the action to use.

Return value

\Drupal\Core\Config\Entity\ConfigEntityInterface The unsaved entity with the action applied.

2 calls to EntityMethod::applySingle()
EntityMethod::apply in core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/EntityMethod.php
Applies the config action.
EntityMethod::applyPluralized in core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/EntityMethod.php
Applies the action to entity treating the $values array as multiple calls.

File

core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/EntityMethod.php, line 132

Class

EntityMethod
Makes config entity methods with the ActionMethod attribute into actions.

Namespace

Drupal\Core\Config\Action\Plugin\ConfigAction

Code

private function applySingle(ConfigEntityInterface $entity, mixed $value) : ConfigEntityInterface {
    // If $value is not an array then we only support calling the method if the
    // number of parameters or required parameters is 1. If there is only 1
    // parameter and $value is an array then assume that the parameter expects
    // an array.
    if (!is_array($value) || $this->numberOfParams === 1) {
        if ($this->numberOfRequiredParams !== 1 && $this->numberOfParams !== 1) {
            throw new EntityMethodException(sprintf('Entity method config action \'%s\' requires an array value. The number of parameters or required parameters for %s::%s() is not 1', $this->pluginId, $entity->getEntityType()
                ->getClass(), $this->method));
        }
        $result = $entity->{$this->method}($value);
    }
    else {
        $result = $entity->{$this->method}(...$value);
    }
    // If an instance of the entity (either itself, or a clone) was returned
    // by the method, return that.
    return is_a($result, get_class($entity)) ? $result : $entity;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.