You are here

class SiteSettingReplicateForm in Site Settings and Labels 8

Class SiteSettingReplicateForm.

@package Drupal\site_settings\Form

Hierarchy

Expanded class hierarchy of SiteSettingReplicateForm

2 string references to 'SiteSettingReplicateForm'
site_settings.routing.yml in ./site_settings.routing.yml
site_settings.routing.yml
site_settings.routing.yml in ./site_settings.routing.yml
site_settings.routing.yml

File

src/Form/SiteSettingReplicateForm.php, line 16

Namespace

Drupal\site_settings\Form
View source
class SiteSettingReplicateForm extends FormBase {

  /**
   * Drupal\Core\Entity\EntityTypeManagerInterface definition.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Drupal\Core\Extension\ModuleHandlerInterface definition.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler) {
    $this->entityTypeManager = $entity_type_manager;
    $this->moduleHandler = $module_handler;
  }

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

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

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

    // Optional dependencies check here as we don't want to force the use of
    // these modules for the core functionality of this module.
    $has_replicate = $this->moduleHandler
      ->moduleExists('replicate');
    $has_field_tools = $this->moduleHandler
      ->moduleExists('field_tools');
    if (!$has_replicate && !$has_field_tools) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Please install the Replicate and Field Tools modules to use this feature.'), 'warning');
    }
    elseif (!$has_replicate) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Please install the Replicate module to use this feature.'), 'warning');
    }
    elseif (!$has_field_tools) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Please install the Field Tools module to use this feature.'), 'warning');
    }
    elseif ($site_setting_entity_type = $this->entityTypeManager
      ->getStorage('site_setting_entity_type')
      ->load($setting)) {

      /** @var \Drupal\site_settings\Entity\SiteSettingEntityType $site_setting_entity_type */

      // The form.
      $form['setting'] = [
        '#type' => 'hidden',
        '#value' => $setting,
      ];
      $form['description'] = [
        '#markup' => '<h2>' . $this
          ->t('Replicating from setting: @setting', [
          '@setting' => $setting,
        ]) . '</h2>',
      ];

      // State that the form needs to allow for a hierarchy.
      $form['#tree'] = TRUE;

      // Initial number of names.
      if (!$form_state
        ->get('num_to_generate')) {
        $form_state
          ->set('num_to_generate', 1);
      }

      // Container for our repeating fields.
      $form['new_settings'] = [
        '#type' => 'table',
        '#header' => [
          $this
            ->t('Machine name'),
          $this
            ->t('Label'),
          $this
            ->t('Fieldset'),
        ],
      ];

      // Add our names fields.
      for ($x = 0; $x < $form_state
        ->get('num_to_generate'); $x++) {
        $form['new_settings'][$x]['machine_name'] = [
          '#type' => 'machine_name',
          '#title' => '',
          '#description' => '',
          '#size' => 30,
          '#required' => FALSE,
          '#machine_name' => [
            'exists' => [
              $this,
              'machineNameExists',
            ],
          ],
        ];
        $form['new_settings'][$x]['label'] = [
          '#type' => 'textfield',
          '#title' => '',
          '#description' => '',
          '#size' => 20,
        ];
        $form['new_settings'][$x]['fieldset'] = [
          '#type' => 'textfield',
          '#title' => '',
          '#description' => '',
          '#size' => 20,
          '#default_value' => $site_setting_entity_type->fieldset,
        ];
      }
      $form['button_container'] = [
        '#type' => 'container',
      ];
      $form['button_container']['add_setting'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Add another setting'),
        '#limit_validation_errors' => [],
      ];
      $form['button_container']['submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Generate settings'),
      ];
    }
    else {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Unable to load setting.'), 'warning');
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
    if ($existing = $this->entityTypeManager
      ->getStorage('site_setting_entity_type')
      ->loadMultiple()) {
      $values = $form_state
        ->getValues();
      foreach ($values['new_settings'] as $key => $setting) {

        // Skip if all empty and not first row.
        if ($key > 0 && empty($setting['machine_name']) && empty($setting['label']) && empty($setting['fieldset'])) {
          continue;
        }

        // Double check that all 3 are filled in if any 1 is.
        if (empty($setting['machine_name'])) {
          $form_state
            ->setErrorByName('new_settings][' . $key . '][machine_name', $this
            ->t('Please enter a machine name'));
        }
        if (empty($setting['label'])) {
          $form_state
            ->setErrorByName('new_settings][' . $key . '][label', $this
            ->t('Please enter a label'));
        }
        if (empty($setting['fieldset'])) {
          $form_state
            ->setErrorByName('new_settings][' . $key . '][fieldset', $this
            ->t('Please enter a fieldset'));
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function machineNameExists($value, array $form, FormStateInterface $form_state) {
    $matches = 0;

    // Check if exists.
    if ($existing = $this->entityTypeManager
      ->getStorage('site_setting_entity_type')
      ->loadMultiple()) {
      if (in_array($value, $existing)) {
        $matches++;
      }
    }

    // Check if the new ones we are adding match.
    $values = $form_state
      ->getValues();
    foreach ($values['new_settings'] as $key => $setting) {
      if ($setting['machine_name'] == $value) {
        $matches++;
      }
    }

    // 1 match is allowed as that is self.
    if ($matches > 1) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }

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

    // Decide what action to take based on which button the user clicked.
    switch ($values['op']) {
      case 'Add another setting':
        $this
          ->addNewFields($form, $form_state);
        break;
      default:
        $this
          ->finalSubmit($form, $form_state);
    }
  }

  /**
   * Handle adding new.
   */
  private function addNewFields(array &$form, FormStateInterface $form_state) {

    // Add 1 to the number of names.
    $num_to_generate = $form_state
      ->get('num_to_generate');
    $form_state
      ->set('num_to_generate', $num_to_generate + 1);

    // Rebuild the form.
    $form_state
      ->setRebuild();
  }

  /**
   * Handle submit.
   */
  private function finalSubmit(array &$form, FormStateInterface $form_state) {

    // Path to the batch processing.
    $path = drupal_get_path('module', 'site_settings');
    $path .= '/src/SiteSettingsReplicateBatches.php';

    // Information to pass to the batch processing.
    $settings = [
      'values' => $form_state
        ->getValues(),
    ];
    $batch = [
      'title' => $this
        ->t('Exporting'),
      'operations' => [
        [
          '_site_settings_replicate_process_batch',
          [
            $settings,
          ],
        ],
      ],
      'finished' => '_site_settings_replicate_finish_batch',
      'file' => $path,
    ];
    batch_set($batch);
  }

}

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::$configFactory protected property The config factory. 1
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.
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.
SiteSettingReplicateForm::$entityTypeManager protected property Drupal\Core\Entity\EntityTypeManagerInterface definition.
SiteSettingReplicateForm::$moduleHandler protected property Drupal\Core\Extension\ModuleHandlerInterface definition.
SiteSettingReplicateForm::addNewFields private function Handle adding new.
SiteSettingReplicateForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
SiteSettingReplicateForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
SiteSettingReplicateForm::finalSubmit private function Handle submit.
SiteSettingReplicateForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SiteSettingReplicateForm::machineNameExists public function
SiteSettingReplicateForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
SiteSettingReplicateForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
SiteSettingReplicateForm::__construct public function
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.