Advertising sustains the DA. Ads are hidden for members. Join today

On this page

Example using a dynamic variable and dependency injection

Last updated on
2 October 2019

As an example, the content type machine name (e.g. 'page') could be replaced with a variable $content_type in the generated code. Let's say, if we are inside a class now using dependency injection, the first line to set our configuration object would look like:

use Drupal\Core\Config\ConfigFactoryInterface;

...

$this->configFactory->getEditable('language.content_settings.node.' . $content_type)->setData([

Also other occurrences of 'page' should also be replaced with $content_type like:

  'dependencies' => [
    'config' => ['node.type.' . $content_type],
  'id' => 'node.' . $content_type,
  'target_bundle' => $content_type,

Wrapping the whole code snippet then inside a foreach loop to set a certain configuration for each content-type:

        use Drupal\Core\Entity\EntityTypeBundleInfoInterface;

        // Enable translations for each existing content type.
        $bundle_info = $this->entityTypeBundleInfo->getBundleInfo('node');
        foreach ($bundle_info as $content_type => $value) {
          ....
        }

Documentation

Simple Configuration API - Writing configuration - Drupal 8 guide on Drupal.org

Help improve this page

Page status: No known problems

You can: