You are here

class SettingsForm in Habitat 8

Defines a form that configures habitat settings.

Hierarchy

Expanded class hierarchy of SettingsForm

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

File

habitat_ui/src/Form/SettingsForm.php, line 16
Contains \Drupal\habitat_ui\Form\SettingsForm.

Namespace

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

  /**
   * An array of configuration names that should be editable.
   *
   * @var array
   */
  protected $editableConfig = [];

  /**
   * {@inheritdoc}
   */
  public function getFormID() {
    return 'habitat_admin_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return $this->editableConfig;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('habitat.settings');
    $habitats = $config
      ->get('habitat_habitats');
    $form['variable'] = array(
      '#type' => 'textfield',
      '#title' => t('Habitat variable name'),
      '#required' => TRUE,
      '#default_value' => $config
        ->get('habitat_variable'),
      '#description' => t('The habitat variable used in your settings.php files to indicate the habitat. This should be placed into settings.php like $settings[\'!variable\'] = \'dev\'. Defaults to \'fetcher_environment\' which is added to settings.php when sites are built with the !fetcher system.', array(
        '!variable' => $config
          ->get('habitat_variable'),
        '!fetcher' => '<a href="http://drupal.org/project/fetcher">fetcher</a>',
      )),
    );
    $form['habitats'] = array(
      '#type' => 'textarea',
      '#title' => t('Habitats'),
      '#description' => t('The habitats to manage. Use machine_name conventions and enter one per line.'),
      '#required' => TRUE,
      '#default_value' => implode("\n", $habitats),
    );
    foreach ($habitats as $habitat) {
      $form['install_' . $habitat] = array(
        '#type' => 'textarea',
        '#title' => t('%habitat installed modules', array(
          '%habitat' => $habitat,
        )),
        '#description' => t('The modules to force install in this habitat. Use machine_name conventions and enter one per line.'),
        '#default_value' => implode("\n", $config
          ->get('habitat_install_' . $habitat)),
      );
      $form['uninstall_' . $habitat] = array(
        '#type' => 'textarea',
        '#title' => t('%habitat uninstalled modules', array(
          '%habitat' => $habitat,
        )),
        '#description' => t('The modules to force uninstall in this habitat. Use machine_name conventions and enter one per line.'),
        '#default_value' => implode("\n", $config
          ->get('habitat_uninstall_' . $habitat)),
      );
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = \Drupal::configFactory()
      ->getEditable('habitat.settings');
    $config
      ->set('habitat_variable', $form_state
      ->getValue('variable'));
    $habitats = array_filter(array_map('trim', explode("\n", $form_state
      ->getValue('habitats'))));
    $config
      ->set('habitat_habitats', $habitats);
    $original_habitats = $config
      ->get('habitat_habitats');
    foreach ($original_habitats as $original_habitat) {
      if (in_array($original_habitat, $habitats)) {
        $config
          ->set('habitat_install_' . $original_habitat, array_filter(array_map('trim', explode("\n", $form_state
          ->getValue('install_' . $original_habitat)))));
        $config
          ->set('habitat_uninstall_' . $original_habitat, array_filter(array_map('trim', explode("\n", $form_state
          ->getValue('uninstall_' . $original_habitat)))));
      }
      else {
        $config
          ->clear('habitat_install_' . $original_habitat);
        $config
          ->clear('habitat_uninstall_' . $original_habitat);
      }
    }
    $config
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('habitat.settings');
    $habitats = $config
      ->get('habitat_habitats');
    $module_data = system_rebuild_module_data();
    foreach ($habitats as $habitat) {
      foreach (array(
        'install',
        'uninstall',
      ) as $type) {
        $modules = array_filter(array_map('trim', explode("\n", $form_state
          ->getValue($type . '_' . $habitat))));
        $module_list = $modules ? array_combine($modules, $modules) : array();
        if ($missing_modules = array_diff_key($module_list, $module_data)) {
          $form_state
            ->setErrorByName($type . '_' . $habitat, t('Cannot set @habitat @typeed modules due to missing modules @modules.', array(
            '@habitat' => $habitat,
            '@type' => $type,
            '@modules' => implode(', ', $missing_modules),
          )));
        }
      }
    }
  }

}

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.
FormInterface::getFormId public function Returns a unique string identifying the form. 236
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::$editableConfig protected property An array of configuration names that should be editable.
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
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
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.