You are here

social_landing_page.install in Open Social 8.8

Install, update and uninstall functions for the social_landing_page module.

File

modules/social_features/social_landing_page/social_landing_page.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the social_landing_page module.
 */
use Drupal\Core\Extension\Extension;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\FieldConfigInterface;
use Drupal\user\Entity\Role;

/**
 * Implements hook_install().
 *
 * Perform actions related to the installation of social_landing_page.
 */
function social_landing_page_install() {

  // Set some default permissions.
  _social_landing_page_set_permissions();
}

/**
 * Function to set permissions.
 */
function _social_landing_page_set_permissions() {
  $roles = Role::loadMultiple();

  /** @var \Drupal\user\Entity\Role $role */
  foreach ($roles as $role) {
    if ($role
      ->id() === 'administrator') {
      continue;
    }
    $permissions = _social_landing_page_get_permissions($role
      ->id());
    user_role_grant_permissions($role
      ->id(), $permissions);
  }
}

/**
 * Build the permissions.
 */
function _social_landing_page_get_permissions($role) {

  // Anonymous.
  $permissions['anonymous'] = [
    'view node.landing_page.field_content_visibility:public content',
  ];

  // Authenticated.
  $permissions['authenticated'] = array_merge($permissions['anonymous'], [
    'view node.landing_page.field_content_visibility:community content',
  ]);

  // Content manager.
  $permissions['contentmanager'] = array_merge($permissions['authenticated'], [
    'create landing_page content',
    'delete any landing_page content',
    'delete own landing_page content',
    'edit any landing_page content',
    'edit own landing_page content',
    'view landing_page revisions',
    'delete landing_page revisions',
    'revert landing_page revisions',
    'override landing_page revision log entry',
    'override landing_page authored by option',
    'override landing_page published option',
    'override landing_page authored on option',
    'override landing_page promote to front landing_page option',
    'override landing_page revision option',
    'override landing_page sticky option',
    'view node.landing_page.field_content_visibility:group content',
    'link to any page',
  ]);

  // Site manager.
  $permissions['sitemanager'] = array_merge($permissions['contentmanager'], []);
  if (isset($permissions[$role])) {
    return $permissions[$role];
  }
  return [];
}

/**
 * Enable the Social Featured Items module.
 */
function social_landing_page_update_8801() {
  $module = 'social_featured_items';

  // We don't actually install the module because this would cause issues with
  // configuration already existing. The configuration has been moved from this
  // module to the separate sub module so we simply mark the module as enabled.
  $extension_config = \Drupal::configFactory()
    ->getEditable('core.extension');

  // Only enable the module if it's not installed already.
  if ($extension_config
    ->get("module.{$module}") !== NULL) {

    // Nothing to do.
    return;
  }

  // Mark the module as enabled. From ModuleInstaller::install().
  $extension_config
    ->set("module.{$module}", 0)
    ->set('module', module_config_sort($extension_config
    ->get('module')))
    ->save(TRUE);

  // Prepare the new module list, sorted by weight, including filenames.
  // This list is used for both the ModuleHandler and DrupalKernel. It
  // needs to be kept in sync between both. A DrupalKernel reboot or
  // rebuild will automatically re-instantiate a new ModuleHandler that
  // uses the new module list of the kernel. However, DrupalKernel does
  // not cause any modules to be loaded.
  // Furthermore, the currently active (fixed) module list can be
  // different from the configured list of enabled modules. For all active
  // modules not contained in the configured enabled modules, we assume a
  // weight of 0.
  $current_module_filenames = \Drupal::moduleHandler()
    ->getModuleList();
  $current_modules = array_fill_keys(array_keys($current_module_filenames), 0);
  $current_modules = module_config_sort(array_merge($current_modules, $extension_config
    ->get('module')));
  $module_filenames = [];
  foreach ($current_modules as $name => $weight) {
    if (isset($current_module_filenames[$name])) {
      $module_filenames[$name] = $current_module_filenames[$name];
    }
    else {
      $module_path = \Drupal::service('extension.list.module')
        ->getPath($name);
      $pathname = "{$module_path}/{$name}.info.yml";
      $filename = file_exists($module_path . "/{$name}.module") ? "{$name}.module" : NULL;
      $module_filenames[$name] = new Extension(\Drupal::root(), 'module', $pathname, $filename);
    }
  }

  // Update the module handler in order to load the module's code.
  // This allows the module to participate in hooks and its existence to
  // be discovered by other modules.
  // The current ModuleHandler instance is obsolete with the kernel
  // rebuild below.
  \Drupal::moduleHandler()
    ->setModuleList($module_filenames);
  \Drupal::moduleHandler()
    ->load($module);

  // No need to module_load_install($module); since there is no such file.
  //
  // Clear the static cache of the "extension.list.module" service to pick
  // up the new module, since it merges the installation status of modules
  // into its statically cached list.
  \Drupal::service('extension.list.module')
    ->reset();

  // Given that we know that the module only contains existing configuration at
  // this point we don't need to go through the rest of the install process.
}

/**
 * Import new block type for featured items.
 */
function social_landing_page_update_8802() {

  // This creates some new configuration that can be used by the dashboard
  // module. This is done in this module because we also enable the
  // social_featured_items module. If that module is enabled through another
  // module then that will take care of the module installation.
  $configs = _social_landing_page_featured_items_config();
  foreach ($configs as $name => $config) {
    \Drupal::configFactory()
      ->getEditable($name)
      ->setData($config)
      ->save(TRUE);
  }
}

/**
 * Returns the featured item block config details.
 *
 * @return array
 *   Contains the config details.
 */
function _social_landing_page_featured_items_config() {
  return [
    'block_content.type.featured_items' => [
      'langcode' => 'en',
      'status' => TRUE,
      'dependencies' => [],
      'id' => 'featured_items',
      'label' => 'Featured Items',
      'revision' => 0,
      'description' => 'A list of featured items that can have a title, link, image/icon, and description.',
    ],
    'field.storage.block_content.field_featured_items' => [
      'langcode' => 'en',
      'status' => TRUE,
      'dependencies' => [
        'module' => [
          'block_content',
          'entity_reference_revisions',
          'paragraphs',
        ],
      ],
      'id' => 'block_content.field_featured_items',
      'field_name' => 'field_featured_items',
      'entity_type' => 'block_content',
      'type' => 'entity_reference_revisions',
      'settings' => [
        'target_type' => 'paragraph',
      ],
      'module' => 'entity_reference_revisions',
      'locked' => FALSE,
      'cardinality' => -1,
      'translatable' => TRUE,
      'indexes' => [],
      'persist_with_no_fields' => FALSE,
      'custom_storage' => FALSE,
    ],
    'field.field.block_content.featured_items.field_featured_items' => [
      'langcode' => 'en',
      'status' => TRUE,
      'dependencies' => [
        'config' => [
          'block_content.type.featured_items',
          'field.storage.block_content.field_featured_items',
          'paragraphs.paragraphs_type.featured_item',
        ],
        'module' => [
          'entity_reference_revisions',
        ],
      ],
      'id' => 'block_content.featured_items.field_featured_items',
      'field_name' => 'field_featured_items',
      'entity_type' => 'block_content',
      'bundle' => 'featured_items',
      'label' => 'Featured Items',
      'description' => '',
      'required' => TRUE,
      'translatable' => TRUE,
      'default_value' => [],
      'default_value_callback' => '',
      'settings' => [
        'handler' => 'default:paragraph',
        'handler_settings' => [
          'negate' => 0,
          'target_bundles' => [
            'featured_item' => 'featured_item',
          ],
          'target_bundles_drag_drop' => [
            'featured_item' => [
              'enabled' => TRUE,
              'weight' => 3,
            ],
            'featured_items' => [
              'weight' => 4,
              'enabled' => FALSE,
            ],
          ],
        ],
      ],
      'field_type' => 'entity_reference_revisions',
    ],
    'core.entity_form_display.block_content.featured_items.default' => [
      'langcode' => 'en',
      'status' => TRUE,
      'dependencies' => [
        'config' => [
          'block_content.type.featured_items',
          'field.field.block_content.featured_items.field_featured_items',
        ],
        'module' => [
          'paragraphs',
        ],
      ],
      'id' => 'block_content.featured_items.default',
      'targetEntityType' => 'block_content',
      'bundle' => 'featured_items',
      'mode' => 'default',
      'content' => [
        'field_featured_items' => [
          'type' => 'entity_reference_paragraphs',
          'weight' => 27,
          'settings' => [
            'title' => 'Paragraph',
            'title_plural' => 'Paragraphs',
            'edit_mode' => 'open',
            'add_mode' => 'dropdown',
            'form_display_mode' => 'default',
            'default_paragraph_type' => '',
          ],
          'third_party_settings' => [],
          'region' => 'content',
        ],
        'info' => [
          'type' => 'string_textfield',
          'weight' => -5,
          'region' => 'content',
          'settings' => [
            'size' => 60,
            'placeholder' => '',
          ],
          'third_party_settings' => [],
        ],
      ],
      'hidden' => [],
    ],
    'core.entity_view_display.block_content.featured_items.default' => [
      'langcode' => 'en',
      'status' => TRUE,
      'dependencies' => [
        'config' => [
          'block_content.type.featured_items',
          'field.field.block_content.featured_items.field_featured_items',
        ],
        'module' => [
          'entity_reference_revisions',
        ],
      ],
      'id' => 'block_content.featured_items.default',
      'targetEntityType' => 'block_content',
      'bundle' => 'featured_items',
      'mode' => 'default',
      'content' => [
        'field_featured_items' => [
          'type' => 'entity_reference_revisions_entity_view',
          'weight' => 1,
          'label' => 'hidden',
          'settings' => [
            'view_mode' => 'default',
            'link' => '',
          ],
          'third_party_settings' => [],
          'region' => 'content',
        ],
      ],
    ],
  ];
}

/**
 * Update fields for featured items.
 */
function social_landing_page_update_8803() {

  // Delete our last attempt in reverse order.
  $configs = array_reverse(_social_landing_page_featured_items_config());
  foreach ($configs as $name => $config) {
    \Drupal::configFactory()
      ->getEditable($name)
      ->delete();
  }

  // Re-create it now with better usage of config.
  // In the last we used the ConfigInterface
  // for Fields and FieldStorage we need to do better.
  $new_config = _social_landing_page_featured_items_config();
  foreach ($new_config as $name => $data) {
    $parts = explode('.', $name);
    switch ($parts[0] . '.' . $parts[1]) {
      case 'field.storage':
        FieldStorageConfig::create($data)
          ->save();
        break;
      case 'field.field':
        $field_config = FieldConfig::loadByName($parts[2], $parts[3], $parts[4]);
        if (!$field_config instanceof FieldConfigInterface) {
          $field_config = FieldConfig::create($data);
        }
        $field_config
          ->save();
        break;
      default:

        // Fallback similar to before.
        \Drupal::configFactory()
          ->getEditable($name)
          ->setData($data)
          ->save(TRUE);
    }
  }
}

/**
 * Add organization and function fields to profile featured teasers.
 */
function social_landing_page_update_8804() {

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('social_landing_page', 'social_landing_page_update_8804');

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

/**
 * Enable the Social Featured Content module.
 */
function social_landing_page_update_8805() {
  $module = 'social_featured_content';

  // We don't actually install the module because this would cause issues with
  // configuration already existing. The configuration has been moved from this
  // module to the separate sub module so we simply mark the module as enabled.
  $extension_config = \Drupal::configFactory()
    ->getEditable('core.extension');

  // Only enable the module if it's not installed already.
  if ($extension_config
    ->get("module.{$module}") !== NULL) {

    // Nothing to do.
    return;
  }

  // Mark the module as enabled. From ModuleInstaller::install().
  $extension_config
    ->set("module.{$module}", 0)
    ->set('module', module_config_sort($extension_config
    ->get('module')))
    ->save(TRUE);

  // Prepare the new module list, sorted by weight, including filenames.
  // This list is used for both the ModuleHandler and DrupalKernel. It
  // needs to be kept in sync between both. A DrupalKernel reboot or
  // rebuild will automatically re-instantiate a new ModuleHandler that
  // uses the new module list of the kernel. However, DrupalKernel does
  // not cause any modules to be loaded.
  // Furthermore, the currently active (fixed) module list can be
  // different from the configured list of enabled modules. For all active
  // modules not contained in the configured enabled modules, we assume a
  // weight of 0.
  $current_module_filenames = \Drupal::moduleHandler()
    ->getModuleList();
  $current_modules = array_fill_keys(array_keys($current_module_filenames), 0);
  $current_modules = module_config_sort(array_merge($current_modules, $extension_config
    ->get('module')));
  $module_filenames = [];
  foreach ($current_modules as $name => $weight) {
    if (isset($current_module_filenames[$name])) {
      $module_filenames[$name] = $current_module_filenames[$name];
    }
    else {
      $module_path = \Drupal::service('extension.list.module')
        ->getPath($name);
      $pathname = "{$module_path}/{$name}.info.yml";
      $filename = file_exists($module_path . "/{$name}.module") ? "{$name}.module" : NULL;
      $module_filenames[$name] = new Extension(\Drupal::root(), 'module', $pathname, $filename);
    }
  }

  // Update the module handler in order to load the module's code.
  // This allows the module to participate in hooks and its existence to
  // be discovered by other modules.
  // The current ModuleHandler instance is obsolete with the kernel
  // rebuild below.
  \Drupal::moduleHandler()
    ->setModuleList($module_filenames);
  \Drupal::moduleHandler()
    ->load($module);

  // No need to module_load_install($module); since there is no such file.
  //
  // Clear the static cache of the "extension.list.module" service to pick
  // up the new module, since it merges the installation status of modules
  // into its statically cached list.
  \Drupal::service('extension.list.module')
    ->reset();

  // Given that we know that the module only contains existing configuration at
  // this point we don't need to go through the rest of the install process.
}

/**
 * Import new block type for featured content.
 */
function social_landing_page_update_8806() {

  // This creates some new configuration that can be used by the dashboard
  // module. This is done in this module because we also enable the
  // social_follow_content module. If that module is enabled through another
  // module then that will take care of the module installation.
  $configs = [
    'block_content.type.featured' => [
      'langcode' => 'en',
      'status' => TRUE,
      'dependencies' => [],
      'id' => 'featured',
      'label' => 'Featured Content',
      'revision' => 0,
      'description' => 'A list of featured content that can have a title, link, group/node/profile, and description.',
    ],
    'field.storage' => [
      'langcode' => 'en',
      'status' => TRUE,
      'dependencies' => [
        'module' => [
          'block_content',
          'entity_reference_revisions',
          'paragraphs',
        ],
      ],
      'id' => 'block_content.field_featured',
      'field_name' => 'field_featured',
      'entity_type' => 'block_content',
      'type' => 'entity_reference_revisions',
      'settings' => [
        'target_type' => 'paragraph',
      ],
      'module' => 'entity_reference_revisions',
      'locked' => FALSE,
      'cardinality' => 1,
      'translatable' => TRUE,
      'indexes' => [],
      'persist_with_no_fields' => FALSE,
      'custom_storage' => FALSE,
    ],
    'field.field' => [
      'langcode' => 'en',
      'status' => TRUE,
      'dependencies' => [
        'config' => [
          'block_content.type.featured',
          'field.storage.block_content.field_featured',
          'paragraphs.paragraphs_type.featured',
        ],
        'module' => [
          'entity_reference_revisions',
        ],
      ],
      'id' => 'block_content.featured.field_featured',
      'field_name' => 'field_featured',
      'entity_type' => 'block_content',
      'bundle' => 'featured',
      'label' => 'Featured Content',
      'description' => '',
      'required' => TRUE,
      'translatable' => FALSE,
      'default_value' => [],
      'default_value_callback' => '',
      'settings' => [
        'handler' => 'default:paragraph',
        'handler_settings' => [
          'negate' => 0,
          'target_bundles' => [
            'featured' => 'featured',
          ],
          'target_bundles_drag_drop' => [
            'featured' => [
              'enabled' => TRUE,
              'weight' => 0,
            ],
          ],
        ],
      ],
      'field_type' => 'entity_reference_revisions',
    ],
    'core.entity_form_display.block_content.featured.default' => [
      'langcode' => 'en',
      'status' => TRUE,
      'dependencies' => [
        'config' => [
          'block_content.type.featured',
          'field.field.block_content.featured.field_featured',
        ],
        'module' => [
          'paragraphs',
        ],
      ],
      'id' => 'block_content.featured.default',
      'targetEntityType' => 'block_content',
      'bundle' => 'featured',
      'mode' => 'default',
      'content' => [
        'field_featured' => [
          'type' => 'entity_reference_paragraphs',
          'weight' => 1,
          'settings' => [
            'title' => 'Paragraph',
            'title_plural' => 'Paragraphs',
            'edit_mode' => 'open',
            'add_mode' => 'dropdown',
            'form_display_mode' => 'default',
            'default_paragraph_type' => '',
          ],
          'third_party_settings' => [],
          'region' => 'content',
        ],
        'info' => [
          'type' => 'string_textfield',
          'weight' => 0,
          'region' => 'content',
          'settings' => [
            'size' => 60,
            'placeholder' => '',
          ],
          'third_party_settings' => [],
        ],
      ],
      'hidden' => [],
    ],
    'core.entity_view_display.block_content.featured.default' => [
      'langcode' => 'en',
      'status' => TRUE,
      'dependencies' => [
        'config' => [
          'block_content.type.featured',
          'field.field.block_content.featured.field_featured',
        ],
        'module' => [
          'entity_reference_revisions',
        ],
      ],
      'id' => 'block_content.featured.default',
      'targetEntityType' => 'block_content',
      'bundle' => 'featured',
      'mode' => 'default',
      'content' => [
        'field_featured' => [
          'type' => 'entity_reference_revisions_entity_view',
          'weight' => 0,
          'label' => 'hidden',
          'settings' => [
            'view_mode' => 'default',
            'link' => '',
          ],
          'third_party_settings' => [],
          'region' => 'content',
        ],
      ],
      'hidden' => [],
    ],
  ];
  $entity_type_ids = [
    'field.storage' => 'field_storage_config',
    'field.field' => 'field_config',
  ];
  foreach ($configs as $name => $data) {
    if (isset($entity_type_ids[$name])) {
      \Drupal::entityTypeManager()
        ->getStorage($entity_type_ids[$name])
        ->create($data)
        ->save();
    }
    else {
      \Drupal::service('config.storage')
        ->write($name, $data);
    }
  }
}

/**
 * Makes the items field multiple.
 */
function social_landing_page_update_8807() {
  $manager = \Drupal::entityDefinitionUpdateManager();
  $storage_definition = $manager
    ->getFieldStorageDefinition('field_featured_items', 'paragraph');
  $storage_definition
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  $manager
    ->updateFieldStorageDefinition($storage_definition);
  $update_helper = \Drupal::service('update_helper.updater');
  $update_helper
    ->executeUpdate('social_landing_page', 'social_landing_page_update_8807');
  return $update_helper
    ->logger()
    ->output();
}

/**
 * Enable helper modules if flexible group and / or social_book are enabled.
 */
function social_landing_page_update_8808() {

  // If we have flexible groups enabled, we want to enable the helper module
  // social_flexible_group_featured which provides a featured view mode to be
  // used on landing pages.
  if (\Drupal::moduleHandler()
    ->moduleExists('social_group_flexible_group')) {
    \Drupal::service('module_installer')
      ->install([
      'social_flexible_group_featured',
    ]);
  }

  // If social_book is enabled, we enable the helper module social_book_featured
  // which provides a featured view mode to be used on landing pages.
  if (\Drupal::moduleHandler()
    ->moduleExists('social_book')) {
    \Drupal::service('module_installer')
      ->install([
      'social_book_featured',
    ]);
  }
}

/**
 * Allow using any URLs in link fields by CM+.
 */
function social_landing_page_update_8809() {
  foreach ([
    'contentmanager',
    'sitemanager',
  ] as $role) {
    user_role_grant_permissions($role, [
      'link to any page',
    ]);
  }
}

/**
 * Enable helper modules if secret group is enabled.
 */
function social_landing_page_update_8810() {

  // If we have secret groups enabled, we want to enable the helper module
  // social_secret_group_featured which provides a featured view mode to be
  // used on landing pages.
  if (\Drupal::moduleHandler()
    ->moduleExists('social_group_secret')) {
    \Drupal::service('module_installer')
      ->install([
      'social_secret_group_featured',
    ]);
  }
}

Functions

Namesort descending Description
social_landing_page_install Implements hook_install().
social_landing_page_update_8801 Enable the Social Featured Items module.
social_landing_page_update_8802 Import new block type for featured items.
social_landing_page_update_8803 Update fields for featured items.
social_landing_page_update_8804 Add organization and function fields to profile featured teasers.
social_landing_page_update_8805 Enable the Social Featured Content module.
social_landing_page_update_8806 Import new block type for featured content.
social_landing_page_update_8807 Makes the items field multiple.
social_landing_page_update_8808 Enable helper modules if flexible group and / or social_book are enabled.
social_landing_page_update_8809 Allow using any URLs in link fields by CM+.
social_landing_page_update_8810 Enable helper modules if secret group is enabled.
_social_landing_page_featured_items_config Returns the featured item block config details.
_social_landing_page_get_permissions Build the permissions.
_social_landing_page_set_permissions Function to set permissions.