Unverified Commit 870f45cd authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch Committed by Mateu Aguiló Bosch
Browse files

Issue #3305506 by e0ipso: Use the new fancy component selector

parent bd66389d
Loading
Loading
Loading
Loading
+0 −18
Original line number Diff line number Diff line
@@ -274,24 +274,6 @@ class ClBlockDialog extends FormBase {
    return $response;
  }

  /**
   * @return mixed
   */
  private function getComponentSelectOptions(): array {
    $settings = \Drupal::config('cl_block.settings');
    $components = $this->componentDiscovery->findAllWithFilters(
      $settings->get('allowed'),
      $settings->get('forbidden'),
      $settings->get('types'),
      $settings->get('statuses'),
    );
    ksort($components);
    return array_map(
      static fn(Component $component) => $component->getMetadata()->getName(),
      $components
    );
  }

  /**
   * Render API callback: builds the component data elements.
   */
+1 −43
Original line number Diff line number Diff line
@@ -2,8 +2,6 @@

namespace Drupal\cl_block\Plugin\EmbedType;

use Drupal\cl_components\Component\Component;
use Drupal\cl_components\Component\ComponentDiscovery;
use Drupal\Core\Form\FormStateInterface;
use Drupal\embed\EmbedType\EmbedTypeBase;

@@ -40,53 +38,13 @@ class ClComponentEmbed extends EmbedTypeBase {
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['component_id'] = [
      '#type' => 'select',
      '#type' => 'cl_component_selector',
      '#title' => $this->t('CL Component'),
      '#options' => $this->getComponentOptions(),
      '#default_value' => $this->configuration['component_id'],
      '#empty_option' => $this->t('- Select one -'),
      '#required' => TRUE,
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);

    $this->configuration['component_id'] = $form_state->getValue('component_id');
  }

  /**
   * Gets the available component options.
   *
   * @return array
   *   The options array ready for a select.
   */
  private function getComponentOptions(): array {
    $components = \Drupal::service(ComponentDiscovery::class)->findAll();
    $options = array_reduce(
      $components,
      static function (array $carry, Component $component) {
        $group = $component->getMetadata()->getGroup();
        $name = $component->getMetadata()->getName();
        $id = $component->getId();
        if (empty($carry[$group])) {
          $carry[$group] = [];
        }
        $carry[$group][$id] = $name;
        return $carry;
      },
      []
    );
    ksort($options);
    foreach ($options as $group => $opts) {
      natsort($options[$group]);
    }
    return $options;
  }

}