You are here

function _social_content_block_update_sorting_options in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_content_block/social_content_block.install \_social_content_block_update_sorting_options()
  2. 10.3.x modules/social_features/social_content_block/social_content_block.install \_social_content_block_update_sorting_options()
  3. 10.0.x modules/social_features/social_content_block/social_content_block.install \_social_content_block_update_sorting_options()
  4. 10.1.x modules/social_features/social_content_block/social_content_block.install \_social_content_block_update_sorting_options()
  5. 10.2.x modules/social_features/social_content_block/social_content_block.install \_social_content_block_update_sorting_options()

Update the allowed values in the sorting options field based on plugins.

2 calls to _social_content_block_update_sorting_options()
social_content_block_modules_installed in modules/social_features/social_content_block/social_content_block.install
Implements hook_modules_installed().
social_content_block_update_8006 in modules/social_features/social_content_block/social_content_block.install
Add event date as sorting option for events.

File

modules/social_features/social_content_block/social_content_block.install, line 24
Install, update and uninstall functions for the social_content_block module.

Code

function _social_content_block_update_sorting_options() {

  /** @var \Drupal\social_content_block\ContentBlockManagerInterface $content_block_manager */
  $content_block_manager = \Drupal::service('plugin.manager.content_block');

  // Retrieve all sort options, removing duplicates and format them to the
  // format of field storage configuration.
  $sort_options_raw = [];
  foreach ($content_block_manager
    ->getDefinitions() as $plugin_id => $plugin_definition) {

    /** @var \Drupal\social_content_block\ContentBlockPluginInterface $plugin */
    $plugin = $content_block_manager
      ->createInstance($plugin_id);
    $sort_options_raw += $plugin
      ->supportedSortOptions();
  }
  $sort_options = [];
  foreach ($sort_options_raw as $value => $label) {
    $sort_options[] = [
      'value' => $value,
      'label' => $label,
    ];
  }

  // Load the existing cnofiguration and update it if it's different.
  $config_name = 'field.storage.block_content.field_sorting';
  $config = \Drupal::configFactory()
    ->getEditable($config_name);
  $config_data = $config
    ->getRawData();
  if ($sort_options !== $config_data['settings']['allowed_values']) {
    $config_data['settings']['allowed_values'] = $sort_options;
    $config
      ->setData($config_data)
      ->save();
    \Drupal::service('entity_field.manager')
      ->clearCachedFieldDefinitions();
  }
}