You are here

class SimpleNodeImporterConfigForm in Simple Node Importer 8

Configuration Form for the Simple Node Importer.

Hierarchy

Expanded class hierarchy of SimpleNodeImporterConfigForm

1 string reference to 'SimpleNodeImporterConfigForm'
simple_node_importer.routing.yml in ./simple_node_importer.routing.yml
simple_node_importer.routing.yml

File

src/Form/SimpleNodeImporterConfigForm.php, line 12

Namespace

Drupal\simple_node_importer\Form
View source
class SimpleNodeImporterConfigForm extends ConfigFormBase {

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

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'simple_node_importer.settings',
    ];
  }
  protected $contentTypes;

  /**
   * Constructs variables.
   *
   * @var array $contentTypes
   *   The information from the GetContentTypes service for this form.
   * @var bool $checkAvailablity
   *   To check the availability of Content Type exists or not.
   */
  public function __construct($contentTypes, $checkAvailablity) {
    $this->contentTypes = $contentTypes;
    $this->checkAvailablity = $checkAvailablity;
  }

  /**
   * Builds config form.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('simple_node_importer.settings');
    $content_type_selected = [];
    $content_type_select = $config
      ->get('content_type_select');
    $entity_type_options = [
      'node' => 'node',
      'user' => 'user',
      'taxonomy' => 'taxonomy',
    ];
    if (!empty($content_type_select)) {
      foreach ($content_type_select as $key => $value) {
        if ($value) {
          $content_type_selected[$key] = $value;
        }
      }
    }
    $form['fieldset_entity_type'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('entity type settings'),
      '#weight' => 1,
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    ];
    $form['fieldset_entity_type']['entity_type_select'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Select entity type'),
      '#default_value' => $config
        ->get('entity_type_select'),
      '#options' => $entity_type_options,
      '#description' => $this
        ->t('Configuration for the entity type to be selected.'),
      '#required' => FALSE,
    ];
    $form['fieldset_content_type'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Content type settings'),
      '#weight' => 1,
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#states' => [
        'visible' => [
          ':input[name="entity_type_select[node]"]' => [
            [
              'checked' => TRUE,
            ],
          ],
        ],
      ],
    ];
    $form['fieldset_content_type']['content_type_select'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Select content type'),
      '#default_value' => isset($content_type_selected) ? $content_type_selected : NULL,
      '#options' => $this->contentTypes,
      '#description' => $this
        ->t('Configuration for the content type to be selected.'),
      '#required' => FALSE,
    ];
    $form['fieldset_user_auto_create_settings'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('User Auto Creation Settings'),
      '#weight' => 1,
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    ];

    // The options to display in our form radio buttons.
    $options = [
      'admin' => $this
        ->t('Set Admin as default author.'),
      'current' => $this
        ->t('Set current user as default author.'),
      'new' => $this
        ->t('Create new user with authenticated role.'),
    ];
    $form['fieldset_user_auto_create_settings']['simple_node_importer_allow_user_autocreate'] = [
      '#type' => 'radios',
      '#options' => $options,
      '#default_value' => $config
        ->get('simple_node_importer_allow_user_autocreate'),
      '#description' => $this
        ->t('User will be set accordingly, if the provided value for author in csv is not avaiable in the system.'),
    ];
    $form['fieldset_taxonomy_term_type'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Taxonomy Term settings'),
      '#weight' => 1,
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    ];
    $form['fieldset_taxonomy_term_type']['simple_node_importer_allow_add_term'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Allow adding new taxonomy terms.'),
      '#default_value' => $config
        ->get('simple_node_importer_allow_add_term'),
      '#description' => $this
        ->t('Check to allow adding term for taxonomy reference fields, if term is not available.'),
    ];
    $form['fieldset_remove_importer'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Node remove settings'),
      '#weight' => 2,
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    ];

    // The options to display in our checkboxes.
    $option = [
      'option' => $this
        ->t('Delete import logs.'),
    ];
    $form['fieldset_remove_importer']['node_delete'] = [
      '#title' => '',
      '#type' => 'checkboxes',
      '#description' => $this
        ->t('Select the checkbox to delete all import logs permanently.'),
      '#options' => $option,
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('simple_node_importer.settings');
    $config
      ->set('entity_type_select', $form_state
      ->getValue('entity_type_select'))
      ->set('content_type_select', $form_state
      ->getValue('content_type_select'))
      ->set('simple_node_importer_allow_user_autocreate', $form_state
      ->getValue('simple_node_importer_allow_user_autocreate'))
      ->set('simple_node_importer_allow_add_term', $form_state
      ->getValue('simple_node_importer_allow_add_term'))
      ->set('node_delete', $form_state
      ->getValue('node_delete'))
      ->save();
    if (method_exists($this, 'submitFormDeleteLogs')) {
      $this
        ->submitFormDeleteLogs($form, $form_state);
    }
    parent::submitForm($form, $form_state);
  }

  /**
   * Delete import logs.
   */
  public function submitFormDeleteLogs(array &$form, FormStateInterface $form_state) {
    if ($this->checkAvailablity) {
      $node_setting = $form_state
        ->getValue([
        'node_delete',
        'deletelog',
      ]);
      $bundle = 'simple_node';
      $query = \Drupal::entityQuery('node');
      $query
        ->condition('status', 1);
      $query
        ->condition('type', $bundle);
      $nids = $query
        ->execute();
      if ($node_setting === 'deletelog' && !empty($nids)) {
        entity_delete_multiple('node', $nids);
        drupal_set_message($this
          ->t('%count nodes has been deleted.', [
          '%count' => count($nids),
        ]));
      }
      elseif ($node_setting === 'deletelog' && empty($nids)) {
        drupal_set_message($this
          ->t("Oops there is nothing to delete"));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('snp.get_services')
      ->getContentTypeList(), $container
      ->get('snp.get_services')
      ->checkAvailablity());
  }

}

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.
SimpleNodeImporterConfigForm::$contentTypes protected property
SimpleNodeImporterConfigForm::buildForm public function Builds config form. Overrides ConfigFormBase::buildForm
SimpleNodeImporterConfigForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SimpleNodeImporterConfigForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SimpleNodeImporterConfigForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SimpleNodeImporterConfigForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SimpleNodeImporterConfigForm::submitFormDeleteLogs public function Delete import logs.
SimpleNodeImporterConfigForm::__construct public function Constructs variables. 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.