Unverified Commit 199f900d authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch Committed by Mateu Aguiló Bosch
Browse files

Issue #3293455 by e0ipso, andypost: Allow altering components created elsewhere

parent 24a87045
Loading
Loading
Loading
Loading
+21 −5
Changes for src/Component/ComponentDiscovery.php: 21 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -148,9 +148,20 @@ class ComponentDiscovery {
    if (isset($this->components)) {
      return array_values($this->components);
    }
    $components_by_key = fn(array $data) => array_reduce(
    $components_by_id = static fn(array $data) => array_reduce(
      $data,
      static fn(array $all, Component $c) => [...$all, $c->getId() => $c],
      static function (array $all, Component $c) {
        // If there are multiple candidates with the same ID only save the one
        // with the lowest weight.
        $existing_component = $all[$c->getId()] ?? NULL;
        if (!$existing_component) {
          return [...$all, $c->getId() => $c];
        }
        assert($existing_component instanceof Component);
        $existing_weight = $existing_component->getMetadata()->getWeight();
        $candidate_weight = $c->getMetadata()->getWeight();
        return $existing_weight > $candidate_weight ? [...$all, $c->getId() => $c] : $all;
      },
      []
    );
    $cache_ids = $this->cache->get('all-cache-ids');
@@ -158,7 +169,7 @@ class ComponentDiscovery {
      $cache_entries = $this->cache->getMultiple($cache_ids->data);
      // Get the data of the cache properties.
      $cache_data = array_map(
        fn(object $item) => $item->data,
        static fn(object $item) => $item->data,
        $cache_entries
      );
      // Ensure cache data deserializes to a Component.
@@ -166,13 +177,13 @@ class ComponentDiscovery {
        $cache_data,
        static fn(mixed $item) => $item instanceof Component
      );
      $this->components = $components_by_key($cached_components);
      $this->components = $components_by_id($cached_components);
      return array_values($this->components);
    }
    $paths = $this->findAllPaths();
    $components = array_map([$this, 'createComponent'], $paths);
    $components = array_filter($components);
    $this->components = $components_by_key($components);
    $this->components = $components_by_id($components);
    $this->cache->set(
      'all-cache-ids',
      array_map(
@@ -367,6 +378,11 @@ class ComponentDiscovery {
      $components,
      static fn(Component $c) => $c->getId() === $id
    );
    // Make sure all components that match the ID are sorted by weight.
    usort(
      $matches,
      static fn(Component $a, Component $b) => $b->getMetadata()->getWeight() - $a->getMetadata()->getWeight()
    );
    $component = reset($matches);
    if ($component instanceof Component) {
      return $component;
+26 −5
Changes for src/Component/ComponentMetadata.php: 26 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -142,6 +142,13 @@ final class ComponentMetadata {
   */
  private string $phpPath;

  /**
   * The weight of the component.
   *
   * @var int
   */
  private int $weight;

  /**
   * ComponentMetadata constructor.
   *
@@ -179,6 +186,7 @@ final class ComponentMetadata {
    $this->status = $metadata_info['status'] ?? static::COMPONENT_STATUS_WIP;
    $this->componentType = $metadata_info['componentType'] ?? static::COMPONENT_TYPE_ORGANISM;
    $this->libraryDependencies = $metadata_info['libraryDependencies'] ?? [];
    $this->weight = (int) ($metadata_info['weight'] ?? 0);

    // Load the PNG.
    $thumbnail_path = sprintf('%s/thumbnail.png', $path);
@@ -192,11 +200,6 @@ final class ComponentMetadata {
    $this->parseSchemaInfo($metadata_info);
    // Parse the hooks.
    $this->safeName = preg_replace('@[^a-z0-9]+@', '_', strtolower($this->machineName));
    $php_file = new ComponentPhpFile($this->path, $this->machineName, $this->safeName);
    $this->phpPath = $php_file->path;
    if ($php_file->hasPhpFile()) {
      $this->hooks = $php_file->parseHooks();
    }
  }

  /**
@@ -505,8 +508,16 @@ final class ComponentMetadata {
   *
   * @return string[]
   *   The hook names.
   *
   * @throws \Drupal\cl_components\Exception\ComponentSyntaxException
   */
  public function getHooks(): array {
    if (isset($this->hooks)) {
      return $this->hooks;
    }
    $php_file = new ComponentPhpFile($this->path, $this->machineName, $this->safeName);
    $this->phpPath = $php_file->path;
    $this->hooks = $php_file->hasPhpFile() ? $php_file->parseHooks() : [];
    return $this->hooks;
  }

@@ -520,4 +531,14 @@ final class ComponentMetadata {
    return $this->phpPath;
  }

  /**
   * Gets the component weight when more than one by ID exist.
   *
   * @return int
   *   The weight.
   */
  public function getWeight(): int {
    return $this->weight;
  }

}
+5 −0
Changes for src/metadata.schema.json: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -62,6 +62,11 @@
        "DEPRECATED"
      ]
    },
    "weight": {
      "type": "integer",
      "title": "Weight",
      "description": "When two components share the same ID the one with the lowest weight will be rendered."
    },
    "variants": {
      "type": "array",
      "title": "Variants",