You are here

social_activity.install in Open Social 8.3

The Social activity install.

File

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

/**
 * @file
 * The Social activity install.
 */
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\message\Entity\MessageTemplate;
use Drupal\block\Entity\Block;
use Drupal\views\Views;

/**
 * Social activity install function.
 */
function social_activity_install() {

  // Add menu items.
  _social_activity_create_menu_links();

  // We can't add this as dependency, because this modules should be installed
  // after config and other activity modules installation.
  $modules = [
    'activity_basics',
    'activity_send_email',
  ];
  \Drupal::service('module_installer')
    ->install($modules);
  _social_activity_create_activity_homepage_block();
}

/**
 * Function to create some menu items.
 */
function _social_activity_create_menu_links() {
  $menu_links = MenuLinkContent::loadMultiple();
  $parent = NULL;

  /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $menu_link */
  foreach ($menu_links as $menu_link) {
    if ($menu_link
      ->label() === 'Explore' && $menu_link
      ->isExpanded()) {
      $parent = 'menu_link_content:' . $menu_link
        ->uuid();
    }
  }
  if (!is_null($parent)) {
    MenuLinkContent::create([
      'title' => t('Community'),
      'link' => [
        'uri' => 'internal:/explore',
      ],
      'menu_name' => 'main',
      'expanded' => FALSE,
      'weight' => 10,
      'parent' => $parent,
    ])
      ->save();
  }
}

/**
 * Place a block for the activity stream homepage.
 */
function social_activity_update_8001() {
  _social_activity_create_activity_homepage_block();
}

/**
 * Create a block for the activity stream homepage.
 */
function _social_activity_create_activity_homepage_block() {

  // Revert view so it has the block display.
  $config_update_service = \Drupal::service('features.config_update');

  // Check if view exists.
  $view = Views::getView('activity_stream');
  if (is_object($view)) {

    // If exists, we revert.
    $config_update_service
      ->revert('view', 'activity_stream');
  }
  else {

    // Otherwise we import.
    $config_update_service
      ->import('view', 'activity_stream');
  }
  $config = \Drupal::configFactory();

  // Do not add a new block if it already exists.
  $block = Block::load('views_block__activity_stream_block_stream_homepage');
  if (!isset($block)) {
    $visibility = [
      'request_path' => [
        'id' => 'request_path',
        'pages' => '/stream',
        'negate' => FALSE,
        'context_mapping' => [],
      ],
    ];
    $settings = [
      'plugin' => 'views_block:activity_stream-block_stream_homepage',
      'region' => 'content',
      'id' => 'views_block__activity_stream_block_stream_homepage',
      'theme' => $config
        ->get('system.theme')
        ->get('default'),
      'label' => '',
      'visibility' => $visibility,
      'weight' => NULL,
    ];
    $values = [];
    foreach ([
      'region',
      'id',
      'theme',
      'plugin',
      'weight',
      'visibility',
    ] as $key) {
      $values[$key] = $settings[$key];

      // Remove extra values that do not belong in the settings array.
      unset($settings[$key]);
    }
    foreach ($values['visibility'] as $id => $visibility) {
      $values['visibility'][$id]['id'] = $id;
    }
    $settings = [
      'id' => 'views_block:activity_stream-block_stream_homepage',
      'label' => '',
      'provider' => 'views',
      'label_display' => '0',
      'views_label' => '',
      'items_per_page' => 'none',
    ];
    $values['settings'] = $settings;
    $block = Block::create($values);
    $block
      ->save();
  }
}

/**
 * Update all existing Message Templates to work with newest Message version.
 */
function social_activity_update_8002() {
  $templates = MessageTemplate::loadMultiple();
  if (!empty($templates)) {
    foreach ($templates as $name => $template) {
      $new_text = [];

      // Check if text is in the wrong format.
      if (!empty($text_array = $template
        ->get('text'))) {
        foreach ($text_array as $text) {
          if (is_string($text)) {
            $new_text[] = [
              'format' => 'full_html',
              'value' => $text,
            ];
          }
        }

        // If all text was ok this would be empty,
        // no need to update in that case.
        if (!empty($new_text)) {
          $templates[$name]
            ->set('text', $new_text);
          $templates[$name]
            ->save();
          $config_name = "message.template.{$template->getConfigTarget()}";
          $config = \Drupal::service('config.factory')
            ->getEditable($config_name);
          if (!empty($config)) {
            $config
              ->set('text', $new_text);
            $config
              ->save();
          }
        }
      }
    }
  }
}

/**
 * Update Dynamic Entity Reference.
 *
 * Related configs to work with latest version of DER.
 */
function social_activity_update_8003() {
  $config = \Drupal::service('config.factory')
    ->getEditable('core.entity_view_display.activity.activity.default');

  // Only update if config exists and this is not a fresh install.
  if (!empty($config) && !empty($settings = $config
    ->get('content.field_activity_entity.settings'))) {
    if (!empty($settings['view_mode'])) {
      $config
        ->clear('content.field_activity_entity.settings');
      $config
        ->set('content.field_activity_entity.settings', social_activity_get_der_settings());
      $config
        ->save();
    }
  }
}

/**
 * Get entity view mode settings for Dynamic Entity Reference update.
 *
 * @return array
 *   View mode settings, keyed by entity machine name.
 */
function social_activity_get_der_settings() {
  $result = [];
  $settings = [
    'activity' => [
      'comment',
      'node',
      'post',
    ],
    'stream' => [
      'group',
    ],
    'default' => [
      'crop',
      'event_enrollment',
      'flagging',
      'message',
      'search_api_task',
      'activity',
      'block_content',
      'menu_link_content',
      'file',
      'group_content',
      'profile',
      'taxonomy_term',
      'user',
    ],
  ];
  foreach ($settings as $view_mode => $entities) {
    foreach ($entities as $entity) {
      $result[$entity]['view_mode'] = $view_mode;
    }
  }
  return $result;
}

Functions

Namesort descending Description
social_activity_get_der_settings Get entity view mode settings for Dynamic Entity Reference update.
social_activity_install Social activity install function.
social_activity_update_8001 Place a block for the activity stream homepage.
social_activity_update_8002 Update all existing Message Templates to work with newest Message version.
social_activity_update_8003 Update Dynamic Entity Reference.
_social_activity_create_activity_homepage_block Create a block for the activity stream homepage.
_social_activity_create_menu_links Function to create some menu items.