You are here

function addtoany_install in AddToAny Share Buttons 8

Same name and namespace in other branches
  1. 7.4 addtoany.module \addtoany_install()
  2. 7 addtoany.module \addtoany_install()
  3. 7.3 addtoany.module \addtoany_install()

Implements hook_install().

File

./addtoany.install, line 14
Install, update and uninstall functions for the AddToAny module.

Code

function addtoany_install() {

  // Build a structured list of node bundles and view modes that have AddToAny
  // enabled by default.
  $node_enable_by_default = [
    'article' => [
      'default',
      'teaser',
    ],
    'page' => [
      'default',
      'teaser',
    ],
  ];

  // Get the config entity storage handler.
  $storage = \Drupal::entityTypeManager()
    ->getStorage('entity_view_display');
  foreach ($node_enable_by_default as $bundle => $viewmodes) {

    // Set a default weight per node type.
    $display_weight = $bundle == 'article' ? 5 : 101;
    foreach ($viewmodes as $viewmode) {

      // Get the config entity for this bundle and view mode.
      $display = $storage
        ->load('node.' . $bundle . '.' . $viewmode);

      // Enable the AddToAny extra field and save the config.
      if (!empty($display)) {
        $display
          ->setComponent('addtoany', [
          'weight' => $display_weight,
        ])
          ->save();
      }
    }
  }
}