4.3 Demonstrate ability to store and retrieve data using code

Review the difference between how Drupal 8 forms retrieve data as opposed to Drupal 7.

Retrieving settings using the configuration API.

Storing and retrieving data


Entities, in Drupal, are objects that are used for persistent storage of content and configuration information. See the Information types topic for an overview of the different types of information, and the Configuration API topic for more about the configuration API.


The Field API allows custom data fields to be attached to Drupal entities and takes care of storing, loading, editing, and rendering field data. Any entity type (node, user, etc.) can use the Field API to make itself "fieldable" and thus allow fields to be attached to it. Other modules can provide a user interface for managing custom fields via a web browser as well as a wide and flexible variety of data type, form element, and display format capabilities.

The Configuration API is one of several methods in Drupal for storing information. See the Information types topic for an overview of the different types of information. The sections below have more information about the configuration API; see https://www.drupal.org/developing/api/8/configuration for more details.

For example

$config = \Drupal::config('example.settings');
// Will print 'Hello'.
print $config->get('message');
// Will print 'en'.
print $config->get('langcode');
Method: \Drupal::service('config.factory')->getEditable()

State API

The basic entry point into the State API is \Drupal::state(), which returns an object of class \Drupal\Core\State\StateInterface. This class has methods for storing and retrieving state information; each piece of state information is associated with a string-valued key.

For example

// Get the state class.
$state = \Drupal::state();
// Find out when cron was last run; the key is 'system.cron_last'.
$time = $state->get('system.cron_last');
// Set the cron run time to the current request time.
$state->set('system.cron_last', REQUEST_TIME);
For more on the State API, see https://www.drupal.org/developing/api/8/state


The Views module is a generalized query and display engine, which can be used to make views (formatted lists, grids, feeds, and other output) of items (often entities, but can be other types of data).

Drupal's database abstraction layer provides a unified database query API that can query different underlying databases. It is built upon PHP's PDO (PHP Data Objects) database API, and inherits much of its syntax and semantics. Besides providing a unified API for database queries, the database abstraction layer also provides a structured way to construct complex queries, and it protects the database by using good security practices.

Other Resources

Simple Configuration API