• RE: Drupal 8: Hello, Configuration Management

    It’s great to start seeing some easy to understand detail on what’s coming in Drupal 8, such as the Drupal 8: Hello, Configuration Management blog post by mtift. While it looks like a big improvement on Drupal 7, there still seems to be a lot of redundant code, particularly in the admin form example. I wondered if I could reduce that a bit.

    The unique code looks to just be the form itself, so could we get away with just defining a class like this:

    <?php
    namespace Drupal\hello\Form;
    use Drupal\system\QuickConfigForm;
    
    class HelloConfigForm extends QuickConfigForm {
        // Can we retrieve this from elsewhere?
        protected $module_name = 'hello'; 
    
        /**
         * {@inheritdoc}
         */
        public function buildForm(array $form, array &$form_state) {
            $form['hello_case'] = array(
                '#type' => 'radios',
                '#title' => t('Configure Hello World Text'),
                // New option so we know where to store the value
                '#variable' => 'case',
                '#options' => array(
                    'upper' => t('UPPER'),
                    'title' => t('Title'),
                ),
                '#description' => t('Choose the case of your "Hello, world!" message.'),
            );
    
            return parent::buildForm($form, $form_state);
        }
    }

    The removed code would then go into the parent class:

    <?php
    // Such a class would be most useful in core
    namespace Drupal\system\QuickConfigForm;
    
    use Drupal\Core\Config\ConfigFactory;
    use Drupal\Core\Extension\ModuleHandler;
    use Drupal\system\SystemConfigFormBase;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    
    /**
     * Allows you to quickly create a form to edit the configuration for a module
     */
    abstract class QuickConfigForm extends SystemConfigFormBase {
        protected $module_name;
    
        /**
         * Constructs a \Drupal\hello\Form\HelloConfigForm object.
         *
         * @param \Drupal\Core\Config\ConfigFactory $config_factory
         *   The factory for configuration objects.
         * @param \Drupal\Core\Extension\ModuleHandler $module_handler
         *   The module handler.
         */
        public function __construct(ConfigFactory $config_factory, ModuleHandler $module_handler) {
            $this->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 getFormID() {
            return 'hello_settings';
        }
    
        /**
         * {@inheritdoc}
         */
        public function buildForm(array $form, array &$form_state) {
            $config = $this->configFactory->get($this->module_name . '.settings');
            foreach ($form as $field_name => $value) {
                if (isset($value['#variable'])) {
                    $form[$field_name]['#default_value'] = $config->get($value['#variable']);
                }
            }
            return parent::buildForm($form, $form_state);
        }
    
        /**
         * {@inheritdoc}
         */
        public function submitForm(array &$form, array &$form_state) {
            $config = $this->configFactory->get($this->module_name . '.settings');
            foreach ($form as $field_name => $value) {
                if (isset($value['#variable'])) {
                    $config->set($value['#variable'], $form_state['values'][$field_name]);
                }
            }
            $config->save();
    
            parent::submitForm($form, $form_state);
        }
    }

    I’ve not run this code so there are probably some errors in in, and the naming and namespaces certainly need to be cleared up, but I’d be interested in working on a class like this if it doesn’t already exist.

    Categories: Drupal

    Tags: