diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index 141686f..a891254 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -628,4 +628,20 @@ public static function accessManager() { return static::$container->get('access_manager'); } + /** + * Returns a class instance with a given class definition. + * + * @param string $definition + * A class name or service name. + * + * @throws \InvalidArgumentException + * If $class is not a valid service identifier and the class does not exist. + * + * @return object + * The instance of the class. + */ + public static function resolve($definition) { + return static::$container->get('class_resolver')->getInstanceFromDefinition($definition); + } + } diff --git a/core/lib/Drupal/Core/Form/FormAlterInterface.php b/core/lib/Drupal/Core/Form/FormAlterInterface.php new file mode 100644 index 0000000..9a3565e --- /dev/null +++ b/core/lib/Drupal/Core/Form/FormAlterInterface.php @@ -0,0 +1,31 @@ +alter($form, $form_state); + * } + * @endcode + */ +interface FormAlterInterface { + + /** + * Alters the form array. + * + * @param array $form + * The form array. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ + public function alter(array &$form, FormStateInterface $form_state); +} diff --git a/core/lib/Drupal/Core/Form/FormAlterSubmitInterface.php b/core/lib/Drupal/Core/Form/FormAlterSubmitInterface.php new file mode 100644 index 0000000..6438ab9 --- /dev/null +++ b/core/lib/Drupal/Core/Form/FormAlterSubmitInterface.php @@ -0,0 +1,27 @@ + 'details', - '#title' => t('Contact settings'), - '#open' => TRUE, - '#weight' => 5, - ); - $account = $form_state->getFormObject()->getEntity(); - if (!\Drupal::currentUser()->isAnonymous() && $account->id()) { - $account_data = \Drupal::service('user.data')->get('contact', $account->id(), 'enabled'); - } - $form['contact']['contact'] = array( - '#type' => 'checkbox', - '#title' => t('Personal contact form'), - '#default_value' => isset($account_data) ? $account_data : \Drupal::config('contact.settings')->get('user_default_enabled'), - '#description' => t('Allow other users to contact you via a personal contact form which keeps your email address hidden. Note that some privileged users such as site administrators are still able to contact you even if you choose to disable this feature.'), - ); - $form['actions']['submit']['#submit'][] = 'contact_user_profile_form_submit'; -} - -/** - * Submit callback for the user profile form to save the contact page setting. - */ -function contact_user_profile_form_submit($form, FormStateInterface $form_state) { - $account = $form_state->getFormObject()->getEntity(); - if ($account->id() && $form_state->hasValue('contact')) { - \Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $form_state->getValue('contact')); - } + /** @var \Drupal\contact\Form\UserFormAlter $alter */ + $alter = \Drupal::resolve('Drupal\contact\Form\UserFormAlter'); + $alter->alter($form, $form_state); } /** @@ -181,32 +157,10 @@ function contact_user_profile_form_submit($form, FormStateInterface $form_state) * * Add the default personal contact setting on the user settings page. * - * @see user_admin_settings() + * @see \Drupal\user\AccountSettingsForm */ function contact_form_user_admin_settings_alter(&$form, FormStateInterface $form_state) { - $form['contact'] = array( - '#type' => 'details', - '#title' => t('Contact settings'), - '#open' => TRUE, - '#weight' => 0, - ); - $form['contact']['contact_default_status'] = array( - '#type' => 'checkbox', - '#title' => t('Enable the personal contact form by default for new users'), - '#description' => t('Changing this setting will not affect existing users.'), - '#default_value' => \Drupal::config('contact.settings')->get('user_default_enabled'), - ); - // Add submit handler to save contact configuration. - $form['#submit'][] = 'contact_form_user_admin_settings_submit'; -} - -/** - * Form submission handler for user_admin_settings(). - * - * @see contact_form_user_admin_settings_alter() - */ -function contact_form_user_admin_settings_submit($form, FormStateInterface $form_state) { - \Drupal::config('contact.settings') - ->set('user_default_enabled', $form_state->getValue('contact_default_status')) - ->save(); + /** @var \Drupal\contact\Form\UserAccountSettingsFormAlter $alter */ + $alter = \Drupal::resolve('Drupal\contact\Form\UserAccountSettingsFormAlter'); + $alter->alter($form, $form_state); } diff --git a/core/modules/contact/src/Form/UserAccountSettingsFormAlter.php b/core/modules/contact/src/Form/UserAccountSettingsFormAlter.php new file mode 100644 index 0000000..4cbcb07 --- /dev/null +++ b/core/modules/contact/src/Form/UserAccountSettingsFormAlter.php @@ -0,0 +1,74 @@ +configFactory = $config_factory; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory') + ); + } + + /** + * {@inheritdoc} + */ + public function alter(array &$form, FormStateInterface $form_state) { + $form['contact'] = array( + '#type' => 'details', + '#title' => $this->t('Contact settings'), + '#open' => TRUE, + '#weight' => 0, + ); + $form['contact']['contact_default_status'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Enable the personal contact form by default for new users'), + '#description' => $this->t('Changing this setting will not affect existing users.'), + // @see \Drupal\Core\Form\ConfigFormBase::config() + '#default_value' => $this->configFactory->get('contact.settings')->getOriginal('user_default_enabled', FALSE), + ); + // Add submit handler to save contact configuration. + $form['#submit'][] = array($this, 'submit'); + } + + /** + * {@inheritdoc} + */ + public function submit(array &$form, FormStateInterface $form_state) { + $this->configFactory->get('contact.settings') + ->set('user_default_enabled', $form_state->getValue('contact_default_status')) + ->save(); + } + +} diff --git a/core/modules/contact/src/Form/UserFormAlter.php b/core/modules/contact/src/Form/UserFormAlter.php new file mode 100644 index 0000000..d43c895 --- /dev/null +++ b/core/modules/contact/src/Form/UserFormAlter.php @@ -0,0 +1,87 @@ +currentUser = $current_user; + $this->userData = $user_data; + $this->configFactory = $config_factory; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('current_user'), + $container->get('user.data'), + $container->get('config.factory') + ); + } + + /** + * {@inheritdoc} + */ + public function alter(array &$form, FormStateInterface $form_state) { + $form['contact'] = array( + '#type' => 'details', + '#title' => $this->t('Contact settings'), + '#open' => TRUE, + '#weight' => 5, + ); + $account = $form_state->getFormObject()->getEntity(); + if (!$this->currentUser->isAnonymous() && $account->id()) { + $account_data = $this->userData->get('contact', $account->id(), 'enabled'); + } + $form['contact']['contact'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Personal contact form'), + '#default_value' => isset($account_data) ? $account_data : $this->configFactory->get('contact.settings')->get('user_default_enabled'), + '#description' => $this->t('Allow other users to contact you via a personal contact form which keeps your email address hidden. Note that some privileged users such as site administrators are still able to contact you even if you choose to disable this feature.'), + ); + $form['actions']['submit']['#submit'][] = array($this, 'submit'); + } + + /** + * {@inheritdoc} + */ + public function submit(array &$form, FormStateInterface $form_state) { + $account = $form_state->getFormObject()->getEntity(); + if ($account->id() && $form_state->hasValue('contact')) { + $this->userData->set('contact', $account->id(), 'enabled', (int) $form_state->getValue('contact')); + } + } + +} diff --git a/core/modules/dblog/dblog.module b/core/modules/dblog/dblog.module index e8568d4..07d4fe9 100644 --- a/core/modules/dblog/dblog.module +++ b/core/modules/dblog/dblog.module @@ -94,23 +94,7 @@ function _dblog_get_message_types() { * Implements hook_form_FORM_ID_alter() for system_logging_settings(). */ function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) { - $row_limits = array(100, 1000, 10000, 100000, 1000000); - $form['dblog_row_limit'] = array( - '#type' => 'select', - '#title' => t('Database log messages to keep'), - '#default_value' => \Drupal::config('dblog.settings')->get('row_limit'), - '#options' => array(0 => t('All')) + array_combine($row_limits, $row_limits), - '#description' => t('The maximum number of messages to keep in the database log. Requires a cron maintenance task.', array('@cron' => \Drupal::url('system.status'))) - ); - - $form['#submit'][] = 'dblog_logging_settings_submit'; -} - -/** - * Form submission handler for system_logging_settings(). - * - * @see dblog_form_system_logging_settings_alter() - */ -function dblog_logging_settings_submit($form, FormStateInterface $form_state) { - \Drupal::config('dblog.settings')->set('row_limit', $form_state->getValue('dblog_row_limit'))->save(); + /** @var \Drupal\dblog\Form\SystemLoggingSettingsFormAlter $alter */ + $alter = \Drupal::resolve('Drupal\dblog\Form\SystemLoggingSettingsFormAlter'); + $alter->alter($form, $form_state); } diff --git a/core/modules/dblog/src/Form/SystemLoggingSettingsFormAlter.php b/core/modules/dblog/src/Form/SystemLoggingSettingsFormAlter.php new file mode 100644 index 0000000..520e314 --- /dev/null +++ b/core/modules/dblog/src/Form/SystemLoggingSettingsFormAlter.php @@ -0,0 +1,73 @@ +configFactory = $config_factory; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory') + ); + } + + /** + * {@inheritdoc} + */ + public function alter(array &$form, FormStateInterface $form_state) { + $row_limits = array(100, 1000, 10000, 100000, 1000000); + $form['dblog_row_limit'] = array( + '#type' => 'select', + '#title' => $this->t('Database log messages to keep'), + '#default_value' => $this->configFactory->get('dblog.settings')->getOriginal('row_limit', FALSE), + '#options' => array(0 => $this->t('All')) + array_combine($row_limits, $row_limits), + '#description' => $this->t('The maximum number of messages to keep in the database log. Requires a cron maintenance task.', array('@cron' => $this->url('system.status'))) + ); + $form['#submit'][] = array($this, 'submit'); + } + + /** + * {@inheritdoc} + */ + public function submit(array &$form, FormStateInterface $form_state) { + $this->configFactory->get('dblog.settings')->set('row_limit', $form_state->getValue('dblog_row_limit'))->save(); + } + +} diff --git a/core/modules/language/src/Form/ContentLanguageSettingsForm.php b/core/modules/language/src/Form/ContentLanguageSettingsForm.php index 1e89850..67b455f 100644 --- a/core/modules/language/src/Form/ContentLanguageSettingsForm.php +++ b/core/modules/language/src/Form/ContentLanguageSettingsForm.php @@ -136,7 +136,15 @@ public function buildForm(array $form, FormStateInterface $form_state) { } } - $form = parent::buildForm($form, $form_state); + $form['actions']['#type'] = 'actions'; + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => $this->t('Save configuration'), + '#button_type' => 'primary', + ); + + // By default, render the form using theme_system_config_form(). + $form['#theme'] = 'system_config_form'; return $form; } diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 3a75398..adfb167 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -835,27 +835,12 @@ function node_page_top(array &$page) { * * Alters the theme form to use the admin theme on node editing. * - * @see node_form_system_themes_admin_form_submit() + * @see \Drupal\system\Form\ThemeAdminForm */ -function node_form_system_themes_admin_form_alter(&$form, FormStateInterface $form_state, $form_id) { - $form['admin_theme']['use_admin_theme'] = array( - '#type' => 'checkbox', - '#title' => t('Use the administration theme when editing or creating content'), - '#default_value' => \Drupal::config('node.settings')->get('use_admin_theme'), - ); - $form['#submit'][] = 'node_form_system_themes_admin_form_submit'; -} - -/** - * Form submission handler for system_themes_admin_form(). - * - * @see node_form_system_themes_admin_form_alter() - */ -function node_form_system_themes_admin_form_submit($form, FormStateInterface $form_state) { - \Drupal::config('node.settings') - ->set('use_admin_theme', $form_state->getValue('use_admin_theme')) - ->save(); - \Drupal::service('router.builder_indicator')->setRebuildNeeded(); +function node_form_system_themes_admin_form_alter(&$form, FormStateInterface $form_state) { + /** @var \Drupal\node\Form\SystemThemesAdminFormAlter $alter */ + $alter = \Drupal::resolve('Drupal\node\Form\SystemThemesAdminFormAlter'); + $alter->alter($form, $form_state); } /** diff --git a/core/modules/node/src/Form/SystemThemesAdminFormAlter.php b/core/modules/node/src/Form/SystemThemesAdminFormAlter.php new file mode 100644 index 0000000..030176c --- /dev/null +++ b/core/modules/node/src/Form/SystemThemesAdminFormAlter.php @@ -0,0 +1,87 @@ +configFactory = $config_factory; + $this->routeBuilderIndicator = $indicator; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('router.builder_indicator') + ); + } + + /** + * {@inheritdoc} + */ + public function alter(array &$form, FormStateInterface $form_state) { + $form['admin_theme']['use_admin_theme'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Use the administration theme when editing or creating content'), + '#default_value' => $this->configFactory->get('node.settings')->getOriginal('use_admin_theme', FALSE), + ); + $form['#submit'][] = array($this, 'submit'); + } + + /** + * {@inheritdoc} + */ + public function submit(array &$form, FormStateInterface $form_state) { + $this->configFactory->get('node.settings') + ->set('use_admin_theme', $form_state->getValue('use_admin_theme')) + ->save(); + $this->routeBuilderIndicator->setRebuildNeeded(); + } + +} diff --git a/core/modules/syslog/src/Form/SystemLoggingSettingsFormAlter.php b/core/modules/syslog/src/Form/SystemLoggingSettingsFormAlter.php new file mode 100644 index 0000000..5a8ea5b --- /dev/null +++ b/core/modules/syslog/src/Form/SystemLoggingSettingsFormAlter.php @@ -0,0 +1,132 @@ +configFactory = $config_factory; + $this->moduleHandler = $module_handler; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('module_handler') + ); + } + + /** + * {@inheritdoc} + */ + public function alter(array &$form, FormStateInterface $form_state) { + // Prevent configuration overrides from bleeding into stored configuration. + $old_state = $this->configFactory->setOverrideState(FALSE); + $config = $this->configFactory->get('syslog.settings'); + $this->configFactory->setOverrideState($old_state); + + $help = $this->moduleHandler->moduleExists('help') ? ' ' . $this->l($this->t('More information'), new Url('help.page', ['name' => 'syslog'])) . '.' : NULL; + $form['syslog_identity'] = array( + '#type' => 'textfield', + '#title' => $this->t('Syslog identity'), + '#default_value' => $config->get('identity'), + '#description' => $this->t('A string that will be prepended to every message logged to Syslog. If you have multiple sites logging to the same Syslog log file, a unique identity per site makes it easy to tell the log entries apart.') . $help, + ); + if (defined('LOG_LOCAL0')) { + $form['syslog_facility'] = array( + '#type' => 'select', + '#title' => $this->t('Syslog facility'), + '#default_value' => $config->get('facility'), + '#options' => $this->facilityList(), + '#description' => $this->t('Depending on the system configuration, Syslog and other logging tools use this code to identify or filter messages from within the entire system log.') . $help, + ); + } + $form['syslog_format'] = array( + '#type' => 'textarea', + '#title' => $this->t('Syslog format'), + '#default_value' => $config->get('format'), + '#description' => $this->t('Specify the format of the syslog entry. Available variables are:
!base_url
Base URL of the site.
!timestamp
Unix timestamp of the log entry.
!type
The category to which this message belongs.
!ip
IP address of the user triggering the message.
!request_uri
The requested URI.
!referer
HTTP Referer if available.
!uid
User ID.
!link
A link to associate with the message.
!message
The message to store in the log.
'), + ); + $form['#submit'][] = array($this, 'submit'); + } + + /** + * Lists all possible syslog facilities for UNIX/Linux. + * + * @return array + * An array of syslog facilities for UNIX/Linux. + */ + protected function facilityList() { + return array( + LOG_LOCAL0 => 'LOG_LOCAL0', + LOG_LOCAL1 => 'LOG_LOCAL1', + LOG_LOCAL2 => 'LOG_LOCAL2', + LOG_LOCAL3 => 'LOG_LOCAL3', + LOG_LOCAL4 => 'LOG_LOCAL4', + LOG_LOCAL5 => 'LOG_LOCAL5', + LOG_LOCAL6 => 'LOG_LOCAL6', + LOG_LOCAL7 => 'LOG_LOCAL7', + ); + } + + /** + * {@inheritdoc} + */ + public function submit(array &$form, FormStateInterface $form_state) { + $this->configFactory->get('syslog.settings') + ->set('identity', $form_state->getValue('syslog_identity')) + ->set('facility', $form_state->getValue('syslog_facility')) + ->set('format', $form_state->getValue('syslog_format')) + ->save(); + } + +} diff --git a/core/modules/syslog/syslog.module b/core/modules/syslog/syslog.module index aad4060..d89ed06 100644 --- a/core/modules/syslog/syslog.module +++ b/core/modules/syslog/syslog.module @@ -5,7 +5,6 @@ * Redirects logging messages to syslog. */ -use Drupal\Core\Url; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RouteMatchInterface; @@ -33,61 +32,7 @@ function syslog_help($route_name, RouteMatchInterface $route_match) { * Implements hook_form_FORM_ID_alter(). */ function syslog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) { - $config = \Drupal::config('syslog.settings'); - $help = \Drupal::moduleHandler()->moduleExists('help') ? ' ' . \Drupal::l(t('More information'), new Url('help.page', ['name' => 'syslog'])) . '.' : NULL; - $form['syslog_identity'] = array( - '#type' => 'textfield', - '#title' => t('Syslog identity'), - '#default_value' => $config->get('identity'), - '#description' => t('A string that will be prepended to every message logged to Syslog. If you have multiple sites logging to the same Syslog log file, a unique identity per site makes it easy to tell the log entries apart.') . $help, - ); - if (defined('LOG_LOCAL0')) { - $form['syslog_facility'] = array( - '#type' => 'select', - '#title' => t('Syslog facility'), - '#default_value' => $config->get('facility'), - '#options' => syslog_facility_list(), - '#description' => t('Depending on the system configuration, Syslog and other logging tools use this code to identify or filter messages from within the entire system log.') . $help, - ); - } - $form['syslog_format'] = array( - '#type' => 'textarea', - '#title' => t('Syslog format'), - '#default_value' => $config->get('format'), - '#description' => t('Specify the format of the syslog entry. Available variables are:
!base_url
Base URL of the site.
!timestamp
Unix timestamp of the log entry.
!type
The category to which this message belongs.
!ip
IP address of the user triggering the message.
!request_uri
The requested URI.
!referer
HTTP Referer if available.
!uid
User ID.
!link
A link to associate with the message.
!message
The message to store in the log.
'), - ); - - $form['#submit'][] = 'syslog_logging_settings_submit'; -} - -/** - * Form submission handler for system_logging_settings(). - * - * @see syslog_form_system_logging_settings_alter() - */ -function syslog_logging_settings_submit($form, FormStateInterface $form_state) { - \Drupal::config('syslog.settings') - ->set('identity', $form_state->getValue('syslog_identity')) - ->set('facility', $form_state->getValue('syslog_facility')) - ->set('format', $form_state->getValue('syslog_format')) - ->save(); -} - -/** - * Lists all possible syslog facilities for UNIX/Linux. - * - * @return array - * An array of syslog facilities for UNIX/Linux. - */ -function syslog_facility_list() { - return array( - LOG_LOCAL0 => 'LOG_LOCAL0', - LOG_LOCAL1 => 'LOG_LOCAL1', - LOG_LOCAL2 => 'LOG_LOCAL2', - LOG_LOCAL3 => 'LOG_LOCAL3', - LOG_LOCAL4 => 'LOG_LOCAL4', - LOG_LOCAL5 => 'LOG_LOCAL5', - LOG_LOCAL6 => 'LOG_LOCAL6', - LOG_LOCAL7 => 'LOG_LOCAL7', - ); + /** @var \Drupal\syslog\Form\SystemLoggingSettingsFormAlter $alter */ + $alter = \Drupal::resolve('Drupal\syslog\Form\SystemLoggingSettingsFormAlter'); + $alter->alter($form, $form_state); }