Jan 01 2019
Jan 01

Often a custom module will have configuration settings that can be provided by an administrator. In Drupal 7, the form used for this purpose was provided via form code including a form array, typically encoded in an admin.inc file, and a hook_menu implantation in the .module file. 

It will probably come as no surprise that things are done differently in Drupal 8. So, let's go step-by-step through the creation of a custom module, example, and its settings page.

my_example.info.yml

The .info.yml file is the only file required for a module, and it doesn't need to contain much. Place it in the module root folder, modules/custom/my_example


name: my example
type: module
description: a module to use in my tutorial.
core: '8.x'
configure: my_example.settings

The configure entry uses the routing from the .routing.yml file. This entry results in a configure link being presented when the accordion control for the module is opened on the module admin page.

my_example.routing.yml

The .routing.yml file has a few purposes:

  • Establish the path for the configuration settings page
  • Tie the configuration settings page to its form
  • Declare the permissions necessary to access the page

This file will also be placed in the module root folder.


my_example.settings:
  path: '/admin/config/system/my_example/config'
  defaults:
    _form: '\Drupal\my_example\Form\Settings'
    _title: 'My Example'
  requirements:
    _permission: 'administer site configuration'

In the file, above, note that the prefix to .settings, my_example, matches the module name, and is again used in the _form reference.

my_example.links.menu.yml

Why doesn't my module have a configuration link on the admin configuration page? More often than not, the answer to this and similar questions is that this file wasn't created. The .link.menu.yaml file provides the missing link, so to speak, and like the preceding yaml files should be placed in the module's root folder.


my_example.settings:
  title: 'My Example'
  description: 'A module to use in my tutorial.'
  route_name: my_module.settings
  parent: system.admin_config_system

Note that the first line matches the name used in the .routing.yml file as does the route_name further down in the file. The parent setting determines where on the admin/config page and menu the link will appear, in the System section in this case.

Settings.php

The final file is the one that provides the settings form. Its name matches the last item in the _form setting in the .routing.yml file. It should be placed in the path
/src/Form.


 
namespace Drupal\my_example\Form;
 
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
 
/**
* Configure settings for this site.
*/
class Settings extends ConfigFormBase {
/**
 * {@inheritdoc}
 */
  public function getFormId() {
    return 'my_example_settings';
  }
 
/**
 * {@inheritdoc}
 */
  protected function getEditableConfigNames() {
    return [
      'my_example.settings',
    ];
  }
 
/**
 * {@inheritdoc}
 */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this----->config('my_example.settings');
 
    $form['sample_setting'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Sample setting'),
      '#description' => $this->t('A sample setting for our module.'),
      '#default_value' => $config->get('sample_setting'),
    );
 
    return parent::buildForm($form, $form_state);
  }
 
/**
 * {@inheritdoc}
 */
  public function submitForm(array &$form, FormStateInterface $form_state) {
      // Retrieve the configuration
       $this->configFactory->getEditable('my_example.settings')
      // Set the submitted configuration setting
      ->set('sample_setting', $form_state->getValue('sample_setting'))
      ->save();
 
    parent::submitForm($form, $form_state);
  }
}

A few things to note in this file:

  • The class name matches the form name given in _form setting of the .routing.yml file
  • The method names should always be as listed
  • getFormId() returns the ID of the form
  • getEditableConfigNames() returns the name from the first line of the .routing.yml file
  • buildForm() loads the saved config, if any, based on the same routing entry, and creates the config form
  • The #default_value line in the form item retrieves the last saved value, if any, for that field
  • submitForm() uses the same routing entry and saves the values of the enumerated field(s)
Dec 26 2018
Dec 26

Your step by step guide with easy to follow instructions for navigating Drupal 8.

Book cover of Drupal 8 Quick Start Guide

Available from Amazon and Packt.

Drupal is a powerful content management platform, flexible enough to accommodate almost any content requirements. This flexibility comes with a cost: complexity. Drupal 8 Quick Start Guide will clear your path from installation to a building usable site in minutes, and to a customized site in one sitting.

  • Create and customize Drupal structures, such as menus, tags, and content categories
  • Extend Drupal's capabilities with add-on modules
  • Administer site users, their roles, and the actions to which they are granted access
  • Understand user roles and permissions
  • Create, edit, and publish content
  • Use Views for custom content selection and display
  • Expand your audience with custom RSS feeds
  • Configure a home page with meaningful sections for a better UX
Of course, the Drupal Community isn't the only place to give back. It's a complex world, and there are as many worthy causes as their are melodies, with most being served by multiple charitable organizations. Here are a few of those that strike a 7th chord with me. St. Jude Children's Research Hospital logo UNICEF logo
Apr 27 2017
Apr 27

Thanks to all who took the time to take the poll. The participation was great! Over 350 respondents, which is a number in excess of 10% of the DrupalCon attendance. There were couple comments that I'd like to address:

A few felt that the questions were biased, or that the choices given were biased. I strove to make each of the questions a current topic, without having any imply a leaning in either direction. For example: "How do you feel about Drupal's Leadership" is meant to present a current topic in the community, but is designed to not imply a positive or negative spin. As for the options, it will never be possible in a poll with static response choices to cover all nuances, unless you just give three: positive, negative or neutral. I did try to ensure that each question had a fully positive response, a clearly negative response, and some options in between. I suppose it could have been set up to be a 1-10 scale, but I defy you to qualify the difference between 6 and 7 on such a scale. I also offered to account for comments that included a question number and a custom response when presenting the results, and have done that on questions 1 and 6, for which custom answers were given. 

There was also a concern that question 6 only offered negative or neutral options, and the suggestion was that the question should be removed from the poll as a result. I disagree, feeling that the first option, "The matter was handled properly" is certainly an option that indicates an opinion that is neither negative nor on the fence.

Should I do another poll? Let me know. 

And now, the results:

 The Future of Drupal

 Dries Conflicts

 Drupal Leadership

 Drupal Leadership Structure

 Drupal's Direction

 On the Matter of Crell

 Time Spent with Drupal Hedging your Bet

 Where you work

 What you do

The average time in Drupal for all respondents was 7.1 years.

Apr 20 2017
Apr 20
The State-of-Drupal Poll ayen Thu, 04/20/2017 - 11:25

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web