Commit ae3d3ad7 authored by Maxime Roux's avatar Maxime Roux
Browse files

Issue #3261856 by MacSim, danflanagan8: Manage taxonomy/node dependencies

parent 15bce271
Loading
Loading
Loading
Loading
+88 −46
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ namespace Drupal\index_now\Form;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -35,6 +36,20 @@ class ModuleSettingsForm extends ConfigFormBase {
   */
  protected $uuid;

  /**
   * Tells if the node module is installed.
   *
   * @var bool
   */
  protected $isNodeEnabled;

  /**
   * Tells if the taxonomy module is installed.
   *
   * @var bool
   */
  protected $isTaxonomyEnabled;

  /**
   * The form construct.
   *
@@ -42,18 +57,34 @@ class ModuleSettingsForm extends ConfigFormBase {
   *   The config factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Component\Uuid\UuidInterface $uuid
   *   The uuid service.
   */
  public function __construct(
    ConfigFactoryInterface $config_factory,
    EntityTypeManagerInterface $entity_type_manager,
    ModuleHandlerInterface $module_handler,
    UuidInterface $uuid
  ) {
    parent::__construct($config_factory);
    $this->nodeTypes = $entity_type_manager->getStorage('node_type')->loadMultiple();
    $this->vocabularies = $entity_type_manager->getStorage('taxonomy_vocabulary')->loadMultiple();
    $this->isTaxonomyEnabled = $module_handler->moduleExists('taxonomy');
    $this->isNodeEnabled = $module_handler->moduleExists('node');
    $this->uuid = $uuid;
    $this->vocabularies = [];
    $this->nodeTypes = [];

    if ($this->isTaxonomyEnabled) {
      $this->vocabularies = $entity_type_manager
        ->getStorage('taxonomy_vocabulary')
        ->loadMultiple();
    }
    if ($this->isNodeEnabled) {
      $this->nodeTypes = $entity_type_manager
        ->getStorage('node_type')
        ->loadMultiple();
    }
  }

  /**
@@ -63,6 +94,7 @@ class ModuleSettingsForm extends ConfigFormBase {
    return new static(
      $container->get('config.factory'),
      $container->get('entity_type.manager'),
      $container->get('module_handler'),
      $container->get('uuid')
    );
  }
@@ -110,11 +142,14 @@ class ModuleSettingsForm extends ConfigFormBase {
      ];
    }

    if ($this->isNodeEnabled || $this->isTaxonomyEnabled) {
      $form['tabs'] = [
        '#type' => 'vertical_tabs',
        '#title' => $this->t("Restrict use"),
      ];
    }

    if ($this->isNodeEnabled) {
      $form['node'] = [
        '#type' => 'details',
        '#title' => $this->t("Content types"),
@@ -135,7 +170,9 @@ class ModuleSettingsForm extends ConfigFormBase {
        '#default_value' => $this->configFactory->getEditable('index_now.settings')
          ->get('exclude_node_types'),
      ];
    }

    if ($this->isTaxonomyEnabled) {
      $form['taxonomy'] = [
        '#type' => 'details',
        '#title' => $this->t("Taxonomies"),
@@ -156,6 +193,7 @@ class ModuleSettingsForm extends ConfigFormBase {
        '#default_value' => $this->configFactory->getEditable('index_now.settings')
          ->get('exclude_vocabularies'),
      ];
    }

    $form['default_engine'] = [
      '#type' => 'radios',
@@ -202,11 +240,15 @@ class ModuleSettingsForm extends ConfigFormBase {
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->configFactory->getEditable('index_now.settings')
      ->set('exclude_node_types', $form_state->getValue('exclude_node_types'))
      ->set('exclude_vocabularies', $form_state->getValue('exclude_vocabularies'))
      ->set('default_engine', $form_state->getValue('default_engine'))
      ->save();
    $config = $this->configFactory->getEditable('index_now.settings');
    $config->set('default_engine', $form_state->getValue('default_engine'));
    if ($this->isNodeEnabled) {
      $config->set('exclude_node_types', $form_state->getValue('exclude_node_types'));
    }
    if ($this->isTaxonomyEnabled) {
      $config->set('exclude_vocabularies', $form_state->getValue('exclude_vocabularies'));
    }
    $config->save();

    parent::submitForm($form, $form_state);
  }