You are here

class ScannerAdminForm in Search and Replace Scanner 8

Form for configuring the default scanner settings.

Hierarchy

Expanded class hierarchy of ScannerAdminForm

1 string reference to 'ScannerAdminForm'
scanner.routing.yml in ./scanner.routing.yml
scanner.routing.yml

File

src/Form/ScannerAdminForm.php, line 19

Namespace

Drupal\scanner\Form
View source
class ScannerAdminForm extends ConfigFormBase {

  /**
   * The scanner plugin manager.
   *
   * @var \Drupal\scanner\Plugin\ScannerPluginManager
   */
  protected $scannerPluginManager;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Holds the entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * The entity_type bundle info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * Constructs a ScannerAdminForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\scanner\Plugin\ScannerPluginManager $scanner_plugin_manager
   *   The scanner plugin manager.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity bundle info service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ScannerPluginManager $scanner_plugin_manager, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
    parent::__construct($config_factory);
    $this->scannerPluginManager = $scanner_plugin_manager;
    $this->languageManager = $language_manager;
    $this->entityTypeManager = $entity_type_manager;
    $this->entityFieldManager = $entity_field_manager;
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('plugin.manager.scanner'), $container
      ->get('language_manager'), $container
      ->get('entity_type.manager'), $container
      ->get('entity_field.manager'), $container
      ->get('entity_type.bundle.info'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'scanner_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'scanner.admin_settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('scanner.admin_settings');
    $form['settings'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Default options'),
    ];
    $form['settings']['scanner_mode'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Default: Case sensitive search mode'),
      '#default_value' => $config
        ->get('scanner_mode'),
    ];
    $form['settings']['scanner_wholeword'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Default: Match whole word'),
      '#default_value' => $config
        ->get('scanner_wholeword'),
    ];
    $form['settings']['scanner_regex'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Default: Regular expression search'),
      '#default_value' => $config
        ->get('scanner_regex'),
    ];
    $form['settings']['scanner_published'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Default: Search published nodes only'),
      '#default_value' => $config
        ->get('scanner_published'),
    ];
    $form['settings']['scanner_pathauto'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Default: Maintain custom aliases'),
      '#default_value' => $config
        ->get('scanner_pathauto'),
    ];
    $langs = $this
      ->getLanguages();
    $form['settings']['scanner_language'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Default: Content language'),
      '#options' => $langs,
      '#default_value' => $config
        ->get('scanner_language'),
    ];
    $available_entity_types = $this
      ->getAvailableEntityTypes();
    $form['enabled_content_types'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Enabled entity types'),
      '#options' => $available_entity_types,
      '#default_value' => $config
        ->get('enabled_content_types'),
      '#description' => $this
        ->t('Third party plugins can be written to add more options here.'),
      '#ajax' => [
        'callback' => [
          $this,
          'getFieldsCallback',
        ],
        'event' => 'change',
        'wrapper' => 'content-type-fields',
      ],
    ];
    $enabled_entity_types = $form_state
      ->getValue('enabled_content_types');
    if ($enabled_entity_types === NULL) {
      $enabled_entity_types = $this
        ->config('scanner.admin_settings')
        ->get('enabled_content_types');
    }
    $fields = $this
      ->getEntityTypeFields($available_entity_types, $enabled_entity_types);
    $form['fields_of_selected_content_type'] = [
      '#title' => $this
        ->t('Enabled fields'),
      '#type' => 'checkboxes',
      '#options' => $fields,
      '#default_value' => $config
        ->get('fields_of_selected_content_type'),
      '#prefix' => '<div id="content-type-fields">',
      '#suffix' => '</div>',
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('scanner.admin_settings')
      ->set('scanner_mode', $form_state
      ->getValue('scanner_mode'))
      ->set('scanner_regex', $form_state
      ->getValue('scanner_regex'))
      ->set('scanner_wholeword', $form_state
      ->getValue('scanner_wholeword'))
      ->set('scanner_published', $form_state
      ->getValue('scanner_published'))
      ->set('scanner_pathauto', $form_state
      ->getValue('scanner_pathauto'))
      ->set('scanner_language', $form_state
      ->getValue('scanner_language'))
      ->set('enabled_content_types', array_filter($form_state
      ->getValue('enabled_content_types')))
      ->set('fields_of_selected_content_type', array_filter($form_state
      ->getValue('fields_of_selected_content_type')))
      ->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * AJAX callback for fetching the entity type fields.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   List of entity type fields.
   */
  public function getFieldsCallback(array $form, FormStateInterface $form_state) {
    return $form['fields_of_selected_content_type'];
  }

  /**
   * Gets a list of available entity types as input options.
   *
   * @return array
   *   An array containing the entity type options.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  protected function getAvailableEntityTypes() {
    $options = [];

    // Iterate over the available plugins to get their 'types'.
    foreach ($this->scannerPluginManager
      ->getDefinitions() as $definition) {
      $entity_type_id = $definition['type'];
      try {
        $entity_type = $this->entityTypeManager
          ->getStorage($entity_type_id)
          ->getEntityType();
      } catch (PluginNotFoundException $ignored) {

        // Non-existent entity was provided. Ignore it.
        continue;
      }
      $bundles = $this->entityTypeBundleInfo
        ->getBundleInfo($entity_type_id);
      foreach ($bundles as $bundle_id => $bundle) {
        $options["{$entity_type_id}:{$bundle_id}"] = $this
          ->t('@entity_type » @bundle', [
          '@entity_type' => $entity_type
            ->getLabel(),
          '@bundle' => $bundle['label'],
        ]);
      }
    }
    return $options;
  }

  /**
   * Gets a list of entity fields as input options.
   *
   * @param array $available_entity_types
   *   List of available entity types.
   * @param array $entity_types
   *   The entity types, with their relevant bundles.
   *
   * @return array
   *   An array containing the fields of the entity types.
   */
  protected function getEntityTypeFields(array $available_entity_types, array $entity_types) {
    $options = [];

    // Iterate through each of the selected entity types and get their fields.
    foreach ($entity_types as $key => $value) {
      if (empty($value) || !isset($available_entity_types[$key])) {

        // Ignore the entity type if it's unticked
        // or the entity type no longer exists.
        continue;
      }
      list($entity_type, $bundle) = explode(':', $key);

      // Get the fields for the given entity type and bundle.
      $field_definitions = $this->entityFieldManager
        ->getFieldDefinitions($entity_type, $bundle);
      foreach ($field_definitions as $field_name => $field_definition) {
        $allowed_field_type = [
          // TODO:codebymikey:20200210 why no string_long?
          'string',
          'text_with_summary',
          'text',
          'text_long',
        ];

        // We are only interested in certain field types.
        if (in_array($field_definition
          ->getType(), $allowed_field_type, TRUE)) {

          // Skip fields starting with "parent_" (Paragraphs internal fields).
          if (strpos($field_name, 'parent_') === 0) {
            continue;
          }
          $name_with_type = "{$entity_type}:{$bundle}:{$field_name}";
          $options[$name_with_type] = $this
            ->t('@entity_bundle » @field_label <small><strong>(@field_name)</strong></small>', [
            '@entity_bundle' => $available_entity_types[$key],
            '@field_label' => $field_definition
              ->getLabel(),
            '@field_name' => $field_name,
          ]);
        }
      }
    }
    return $options;
  }

  /**
   * Gets a list of languages as input options.
   *
   * @return array
   *   An array containing the list of enabled languages.
   */
  protected function getLanguages() {
    $languages = $this->languageManager
      ->getLanguages();
    $items = [
      'all' => $this
        ->t('All'),
    ];
    foreach ($languages as $language) {
      $items[$language
        ->getId()] = $language
        ->getName();
    }
    return $items;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
ScannerAdminForm::$entityFieldManager protected property The entity field manager.
ScannerAdminForm::$entityTypeBundleInfo protected property The entity_type bundle info service.
ScannerAdminForm::$entityTypeManager protected property Holds the entity type manager.
ScannerAdminForm::$languageManager protected property The language manager.
ScannerAdminForm::$scannerPluginManager protected property The scanner plugin manager.
ScannerAdminForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
ScannerAdminForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
ScannerAdminForm::getAvailableEntityTypes protected function Gets a list of available entity types as input options.
ScannerAdminForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
ScannerAdminForm::getEntityTypeFields protected function Gets a list of entity fields as input options.
ScannerAdminForm::getFieldsCallback public function AJAX callback for fetching the entity type fields.
ScannerAdminForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ScannerAdminForm::getLanguages protected function Gets a list of languages as input options.
ScannerAdminForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ScannerAdminForm::__construct public function Constructs a ScannerAdminForm object. Overrides ConfigFormBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.