diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index f949669..fa52eec 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -677,4 +677,23 @@ public static function destination() { return static::getContainer()->get('redirect.destination'); } + /** + * Returns a class instance from a given class definition. + * + * @param string $class + * A class name. + * + * @throws \InvalidArgumentException + * Thrown if $class does not exist. + * + * @return object + * The instance of the class. + */ + public static function createInstance($class) { + if (!class_exists($class)) { + throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class)); + } + return static::$container->get('class_resolver')->getInstanceFromDefinition($class); + } + } 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')); - } + \Drupal::createInstance('Drupal\contact\Form\UserFormAlter')->alter($form, $form_state); } /** @@ -202,32 +176,8 @@ function contact_user_profile_form_submit($form, FormStateInterface $form_state) * * Add the default personal contact setting on the user settings page. * - * @see \Drupal\user\AccountSettingsForm + * @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::configFactory()->getEditable('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::configFactory()->getEditable('contact.settings') - ->set('user_default_enabled', $form_state->getValue('contact_default_status')) - ->save(); + \Drupal::createInstance('Drupal\contact\Form\UserAccountSettingsFormAlter')->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 570aaa4..1119c29 100644 --- a/core/modules/dblog/dblog.module +++ b/core/modules/dblog/dblog.module @@ -94,23 +94,5 @@ 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::configFactory()->getEditable('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::configFactory()->getEditable('dblog.settings')->set('row_limit', $form_state->getValue('dblog_row_limit'))->save(); + \Drupal::createInstance('Drupal\dblog\Form\SystemLoggingSettingsFormAlter')->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/node/node.module b/core/modules/node/node.module index f181df1..acefcb7 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -860,15 +860,10 @@ 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::configFactory()->getEditable('node.settings')->get('use_admin_theme'), - ); - $form['#submit'][] = 'node_form_system_themes_admin_form_submit'; +function node_form_system_themes_admin_form_alter(&$form, FormStateInterface $form_state) { + \Drupal::createInstance('Drupal\node\Form\SystemThemesAdminFormAlter')->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 61873f9..32a0a89 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,5 @@ 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::configFactory()->getEditable('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::configFactory()->getEditable('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', - ); + \Drupal::createInstance('Drupal\syslog\Form\SystemLoggingSettingsFormAlter')->alter($form, $form_state); } diff --git a/core/tests/Drupal/Tests/Core/DrupalTest.php b/core/tests/Drupal/Tests/Core/DrupalTest.php index 315ce38..e7c87e5 100644 --- a/core/tests/Drupal/Tests/Core/DrupalTest.php +++ b/core/tests/Drupal/Tests/Core/DrupalTest.php @@ -435,6 +435,33 @@ public function testAccessManager() { } /** + * Tests the createInstance() method. + * + * @covers ::createInstance + */ + public function testCreateInstance() { + $existing_class = get_class($this); + $class_resolver = $this->getMock('\Drupal\Core\DependencyInjection\ClassResolver'); + $this->setMockContainerService('class_resolver', $class_resolver); + $class_resolver->expects($this->any()) + ->method('getInstanceFromDefinition') + ->with($existing_class) + ->willReturn($this); + $this->assertSame($this, \Drupal::createInstance($existing_class)); + } + + /** + * Tests the createInstance() method. + * + * @covers ::createInstance + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Class "\does_not_exist" does not exist. + */ + public function testCreateInstanceException() { + \Drupal::createInstance('\does_not_exist'); + } + + /** * Sets up a mock expectation for the container get() method. * * @param string $service_name