summaryrefslogtreecommitdiffstats
path: root/addanother.install
blob: e61ae15c1ff9aaea4f0c3b322b2baf0ef741ec31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

/**
 * @file
 * Contains install and update functions for addanother module.
 */

use Drupal\node\Entity\NodeType;

/**
 * Implements hook_install().
 */
function addanother_install() {
  _addanother_set_content_type_configs();
}

/**
 * Update existing content with default values if has not  been already set.
 */
function addanother_update_8001() {
  _addanother_set_content_type_configs(TRUE);
}

/**
 * Set addanother options for existing content types.
 */
function _addanother_set_content_type_configs($check_existing = FALSE) {
  $types = array_keys(NodeType::loadMultiple());
  $configs = [
    'button',
    'message',
    'tab',
    'tab_edit',
  ];
  /* @var \Drupal\Core\Config\Config $config_factory  */
  $config_factory = \Drupal::service('config.factory')
    ->getEditable('addanother.settings');

  foreach ($types as $type) {
    foreach ($configs as $config) {
      if ($check_existing &&
        !is_null($config_factory->get("$config.$type"))) {
        continue 2;
      }
      $config_factory->set("$config.$type", $config_factory->get("default_$config"));
    }
  }
  $config_factory->save();
}