function BeforeOrAfter::apply

Overrides OrderOperation::apply

File

core/lib/Drupal/Core/Hook/OrderOperation/BeforeOrAfter.php, line 40

Class

BeforeOrAfter
Moves one listener to be called before or after other listeners.

Namespace

Drupal\Core\Hook\OrderOperation

Code

public function apply(array &$identifiers, array $module_finder) : void {
    assert(array_is_list($identifiers));
    $index = array_search($this->identifier, $identifiers);
    if ($index === FALSE) {
        // Nothing to reorder.
        return;
    }
    $identifiers_to_order_against = $this->identifiersToOrderAgainst;
    if ($this->modulesToOrderAgainst) {
        $identifiers_to_order_against = [
            $identifiers_to_order_against,
            array_keys(array_intersect($module_finder, $this->modulesToOrderAgainst)),
        ];
    }
    $indices_to_order_against = array_keys(array_intersect($identifiers, $identifiers_to_order_against));
    if ($indices_to_order_against === []) {
        return;
    }
    if ($this->isAfter) {
        $max_index_to_order_against = max($indices_to_order_against);
        if ($index >= $max_index_to_order_against) {
            // The element is already after the other elements.
            return;
        }
        array_splice($identifiers, $max_index_to_order_against + 1, 0, $this->identifier);
        // Remove the element after splicing.
        unset($identifiers[$index]);
        $identifiers = array_values($identifiers);
    }
    else {
        $min_index_to_order_against = min($indices_to_order_against);
        if ($index <= $min_index_to_order_against) {
            // The element is already before the other elements.
            return;
        }
        // Remove the element before splicing.
        unset($identifiers[$index]);
        array_splice($identifiers, $min_index_to_order_against, 0, $this->identifier);
    }
}

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