You are here

class SettingsForm in Save & Edit 8

Class SettingsForm.

@package Drupal\save_edit\Form

Hierarchy

Expanded class hierarchy of SettingsForm

1 string reference to 'SettingsForm'
save_edit.routing.yml in ./save_edit.routing.yml
save_edit.routing.yml

File

src/Form/SettingsForm.php, line 14

Namespace

Drupal\save_edit\Form
View source
class SettingsForm extends ConfigFormBase {

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('save_edit.settings');
    $weights_range = range(-10, 10);
    $weights = array_combine($weights_range, $weights_range);
    $form['button_value'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Text to use for Save & Edit button'),
      '#description' => $this
        ->t('This is the default text that will be used for the button at the bottom of the node form.<br>It would be best to use familiar terms like "<strong>Save &amp; Edit</strong>" or "<strong>Apply</strong>" so that users can easily understand the feature/function related to this option.'),
      '#maxlength' => 64,
      '#size' => 64,
      '#default_value' => $config
        ->get('button_value'),
    ];
    $form['button_weight'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Save & Edit Button Weight'),
      '#description' => $this
        ->t('You can adjust the horizontal positioning in the button section (or vertical positioning when using the dropbutton setting).'),
      '#options' => $weights,
      '#default_value' => $config
        ->get('button_weight'),
    ];
    $form['dropbutton'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Integrate into dropbutton'),
      '#description' => $this
        ->t('This setting will insert the Save &amp; Edit button into the save dropbutton.'),
      '#default_value' => $config
        ->get('dropbutton'),
    ];
    $form['unpublish'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Auto Unpublish All Nodes'),
      '#description' => $this
        ->t('This setting will automatically uncheck the "Published" status when using <strong>Save &amp; Edit</strong> button to save nodes.'),
      '#default_value' => $config
        ->get('unpublish'),
    ];
    $form['unpublish_new_only'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Auto Unpublish on New Nodes Only'),
      '#description' => $this
        ->t('This will only mark the node as unpublished upon creating a new node. Assuming this is used, on subsequent uses of <strong>Save &amp; Edit</strong> the node will be unpublished already, and NOT affected. You will be required at some point to manually publish the node using the optional <strong>Publish</strong> button, or manually ticking the appropriate checkbox when hitting the default Save button.'),
      '#default_value' => $config
        ->get('unpublish_new_only'),
    ];
    $form['hide_default_save'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Hide default Save button'),
      '#description' => $this
        ->t('This will hide the Save dropbutton.'),
      '#default_value' => $config
        ->get('hide_default_save'),
    ];
    $form['hide_default_publish'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Hide the Publish button'),
      '#default_value' => $config
        ->get('hide_default_publish'),
      '#description' => $this
        ->t('This will hide the Publish button.'),
    ];
    $form['hide_default_preview'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Hide default Preview button'),
      '#default_value' => $config
        ->get('hide_default_preview'),
      '#description' => $this
        ->t('This will hide the Preview button.'),
    ];
    $form['hide_default_delete'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Hide default Delete button'),
      '#default_value' => $config
        ->get('hide_default_delete'),
      '#description' => $this
        ->t('This will hide the Delete button.'),
    ];
    $node_types = NodeType::loadMultiple();
    $keyed_node_types = [];
    foreach ($node_types as $content_type) {
      $keyed_node_types[$content_type
        ->id()] = $content_type
        ->label();
    }
    $default_value_node_types = $config
      ->get('node_types');
    $form['node_types'] = [
      '#type' => 'checkboxes',
      '#options' => $keyed_node_types,
      '#title' => $this
        ->t('Node types'),
      '#description' => $this
        ->t('Set the node types you want to display links for.'),
      '#default_value' => isset($default_value_node_types) ? $default_value_node_types : [],
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $this
      ->config('save_edit.settings')
      ->set('button_value', $form_state
      ->getValue('button_value'))
      ->set('button_weight', $form_state
      ->getValue('button_weight'))
      ->set('dropbutton', $form_state
      ->getValue('dropbutton'))
      ->set('hide_default_save', $form_state
      ->getValue('hide_default_save'))
      ->set('hide_default_publish', $form_state
      ->getValue('hide_default_publish'))
      ->set('hide_default_preview', $form_state
      ->getValue('hide_default_preview'))
      ->set('hide_default_delete', $form_state
      ->getValue('hide_default_delete'))
      ->set('unpublish', $form_state
      ->getValue('unpublish'))
      ->set('unpublish_new_only', $form_state
      ->getValue('unpublish_new_only'))
      ->set('node_types', $form_state
      ->getValue('node_types'))
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
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.
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
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.