You are here

class NodeTitleValidationAdminForm in Node Title Validation 8

Same name and namespace in other branches
  1. 2.0.x src/Form/NodeTitleValidationAdminForm.php \Drupal\node_title_validation\Form\NodeTitleValidationAdminForm
  2. 1.0.x src/Form/NodeTitleValidationAdminForm.php \Drupal\node_title_validation\Form\NodeTitleValidationAdminForm

Class NodeTitleValidationAdminForm.

@package Drupal\node_title_validation\Form

Hierarchy

Expanded class hierarchy of NodeTitleValidationAdminForm

1 string reference to 'NodeTitleValidationAdminForm'
node_title_validation.routing.yml in ./node_title_validation.routing.yml
node_title_validation.routing.yml

File

src/Form/NodeTitleValidationAdminForm.php, line 16

Namespace

Drupal\node_title_validation\Form
View source
class NodeTitleValidationAdminForm extends FormBase {

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

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a new NodeTitleValidationController.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entityTypeManager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configFactory.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, ConfigFactoryInterface $config_factory) {
    $this->entityTypeManager = $entityTypeManager;
    $this->configFactory = $config_factory;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Get configuration value.
    $node_title_validation_config = $this
      ->config('node_title_validation.node_title_validation_settings')
      ->get('node_title_validation_config');

    // Get available content types.
    $node_types = $this->entityTypeManager
      ->getStorage('node_type')
      ->loadMultiple();

    // Variable to display 1st fieldset collapse open.
    $i = 0;

    // Generate fieldset for each content type along with exclude, min,
    // max and unique form elements.
    foreach ($node_types as $type) {

      // Display First fieldset collapsed open.
      if ($i == 0) {
        $form[$type
          ->get('type')] = [
          '#type' => 'fieldset',
          '#title' => $type
            ->get('name'),
          '#collapsible' => TRUE,
          '#collapsed' => FALSE,
        ];
      }
      else {
        $form[$type
          ->get('type')] = [
          '#type' => 'fieldset',
          '#title' => $type
            ->get('name'),
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
        ];
      }

      // Increment $i for other fieldsets in collapsed closed.
      $i++;
      $form[$type
        ->get('type')]['exclude-' . $type
        ->get('type')] = [
        '#type' => 'textarea',
        '#default_value' => isset($node_title_validation_config['exclude-' . $type
          ->get('type')]) ? $node_title_validation_config['exclude-' . $type
          ->get('type')] : '',
        '#title' => $this
          ->t('Blacklist Characters/Words'),
        '#description' => '<p>' . $this
          ->t("Comma separated characters or words to avoided while saving node title. Ex: !,@,#,\$,%,^,&,*,(,),1,2,3,4,5,6,7,8,9,0,have,has,were,aren't.") . '</p>' . '<p>' . $this
          ->t('If any of the blacklisted characters/words found in node title,would return validation error on node save.') . '</p>',
      ];
      $form[$type
        ->get('type')]['comma-' . $type
        ->get('type')] = [
        '#type' => 'checkbox',
        '#default_value' => isset($node_title_validation_config['comma-' . $type
          ->get('type')]) ? $node_title_validation_config['comma-' . $type
          ->get('type')] : '',
        '#title' => $this
          ->t('Add comma to blacklist.'),
      ];
      $form[$type
        ->get('type')]['min-' . $type
        ->get('type')] = [
        '#type' => 'number',
        '#title' => $this
          ->t("Minimum characters"),
        '#required' => TRUE,
        '#description' => $this
          ->t("Minimum number of characters node title should contain"),
        '#max' => 255,
        '#min' => 1,
        '#default_value' => isset($node_title_validation_config['min-' . $type
          ->get('type')]) ? $node_title_validation_config['min-' . $type
          ->get('type')] : 1,
      ];
      $form[$type
        ->get('type')]['max-' . $type
        ->get('type')] = [
        '#type' => 'number',
        '#title' => $this
          ->t("Maximum characters"),
        '#required' => TRUE,
        '#description' => $this
          ->t("Maximum number of characters node title should contain"),
        '#max' => 255,
        '#min' => 1,
        '#default_value' => isset($node_title_validation_config['max-' . $type
          ->get('type')]) ? $node_title_validation_config['max-' . $type
          ->get('type')] : 255,
      ];
      $form[$type
        ->get('type')]['min-wc-' . $type
        ->get('type')] = [
        '#type' => 'number',
        '#title' => $this
          ->t("Minimum Word Count"),
        '#required' => TRUE,
        '#description' => $this
          ->t("Minimum number of words node title should contain"),
        '#max' => 20,
        '#min' => 1,
        '#default_value' => isset($node_title_validation_config['min-wc-' . $type
          ->get('type')]) ? $node_title_validation_config['min-wc-' . $type
          ->get('type')] : 1,
      ];
      $form[$type
        ->get('type')]['max-wc-' . $type
        ->get('type')] = [
        '#type' => 'number',
        '#title' => $this
          ->t("Maximum Word Count"),
        '#description' => $this
          ->t("Maximum number of words node title should contain"),
        '#max' => 25,
        '#min' => 1,
        '#default_value' => isset($node_title_validation_config['max-wc-' . $type
          ->get('type')]) ? $node_title_validation_config['max-wc-' . $type
          ->get('type')] : 25,
      ];
      $form[$type
        ->get('type')]['unique-' . $type
        ->get('type')] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t("Unique node title for @type content type", [
          '@type' => $type
            ->get('type'),
        ]),
        '#default_value' => isset($node_title_validation_config['unique-' . $type
          ->get('type')]) ? $node_title_validation_config['unique-' . $type
          ->get('type')] : 0,
      ];
    }
    $form['unique'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Unique node title for all content types'),
      '#default_value' => isset($node_title_validation_config['unique']) ? $node_title_validation_config['unique'] : 0,
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // Get available content types.
    $node_types = $this->entityTypeManager
      ->getStorage('node_type')
      ->loadMultiple();

    // Loop for each content type & validate min, max values.
    foreach ($node_types as $type) {

      // Get max characters count.
      $max = $form_state
        ->getValue([
        'max-' . $type
          ->get('type'),
      ]);

      // Get min characters count.
      $min = $form_state
        ->getValue([
        'min-' . $type
          ->get('type'),
      ]);

      // Validate min is less than max value.
      if ($min > $max) {
        $form_state
          ->setErrorByName('min-' . $type
          ->get('type'), $this
          ->t("Minimum length should not be more than Max length"));
      }

      // Get min word count.
      $min_wc = $form_state
        ->getValue('min-wc-' . $type
        ->get('type'));

      // Get max word count.
      $max_wc = $form_state
        ->getValue([
        'max-wc-' . $type
          ->get('type'),
      ]);

      // Validate min is less than max value.
      if (!empty($min_wc) && !empty($max_wc) && $min_wc > $max_wc) {
        $form_state
          ->setErrorByName('max-wc-' . $type
          ->get('type'), $this
          ->t("Minimum word count of title should not be more than Maximum word count"));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = [];

    // Get available content types.
    $node_types = $this->entityTypeManager
      ->getStorage('node_type')
      ->loadMultiple();

    // Store Form values in node_title_validation_config variable.
    foreach ($node_types as $type) {
      $values['comma-' . $type
        ->get('type')] = $form_state
        ->getValue([
        'comma-' . $type
          ->get('type'),
      ]);
      $values['exclude-' . $type
        ->get('type')] = $form_state
        ->getValue([
        'exclude-' . $type
          ->get('type'),
      ]);
      $values['min-' . $type
        ->get('type')] = $form_state
        ->getValue([
        'min-' . $type
          ->get('type'),
      ]);
      $values['max-' . $type
        ->get('type')] = $form_state
        ->getValue([
        'max-' . $type
          ->get('type'),
      ]);
      $values['min-wc-' . $type
        ->get('type')] = $form_state
        ->getValue([
        'min-wc-' . $type
          ->get('type'),
      ]);
      $values['max-wc-' . $type
        ->get('type')] = $form_state
        ->getValue([
        'max-wc-' . $type
          ->get('type'),
      ]);
      $values['unique-' . $type
        ->get('type')] = $form_state
        ->getValue([
        'unique-' . $type
          ->get('type'),
      ]);
    }
    $values['unique'] = $form_state
      ->getValue([
      'unique',
    ]);

    // Set node_title_validation_config variable.
    $this->configFactory
      ->getEditable('node_title_validation.node_title_validation_settings')
      ->set('node_title_validation_config', $values)
      ->save();
    $this
      ->messenger()
      ->addMessage($this
      ->t('Node Title Validation Configurations saved successfully!'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
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.
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.
NodeTitleValidationAdminForm::$configFactory protected property The config factory service. Overrides FormBase::$configFactory
NodeTitleValidationAdminForm::$entityTypeManager protected property The entity type manager.
NodeTitleValidationAdminForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
NodeTitleValidationAdminForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
NodeTitleValidationAdminForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
NodeTitleValidationAdminForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
NodeTitleValidationAdminForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
NodeTitleValidationAdminForm::__construct public function Constructs a new NodeTitleValidationController.
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.
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.