<?php

/**
 * @file
 * Contains install, update and uninstall functions.
 */

/**
 * Implements hook_update_dependencies().
 */
function social_event_enrolments_export_update_dependencies(): array {
  // New config changes should run after the VBO module changes
  // the structure of views.
  $dependencies['social_event_enrolments_export'][11001] = [
    'views_bulk_operations' => 8034,
    'social_event_managers' => 11001,
  ];

  return $dependencies;
}

/**
 * Implements hook_install().
 *
 * Perform actions related to the installation of
 * social_event_enrolments_export.
 */
function social_event_enrolments_export_install(): void {
  // Add changes to event_manage_enrollments view.
  _social_event_enrolments_export_alter_event_manage_enrollments_view();
}

/**
 * Adds a custom action to 'event_manage_enrollments' after VBO module updates.
 *
 * Action ID:  'social_event_enrolments_export_enrollments_action'.
 */
function _social_event_enrolments_export_alter_event_manage_enrollments_view(): void {
  $config = \Drupal::configFactory()->getEditable('views.view.event_manage_enrollments');
  $selected_actions = $config->get('display.default.display_options.fields.social_views_bulk_operations_bulk_form_enrollments_1.selected_actions');
  $selected_actions[] = [
    'action_id' => 'social_event_enrolments_export_enrollments_action',
    'preconfiguration' => [
      'label_override' => t('Export'),
    ],
  ];
  $config->set('display.default.display_options.fields.social_views_bulk_operations_bulk_form_enrollments_1.selected_actions', $selected_actions);
  $config->save();
}

/**
 * Adds a custom action to 'event_manage_enrollments' after VBO module updates.
 *
 * Action ID:  'social_event_enrolments_export_enrollments_action'.
 */
function social_event_enrolments_export_update_11001(array &$sandbox): void {
  $config = \Drupal::configFactory()->getEditable('views.view.event_manage_enrollments');
  $selected_actions = $config->get('display.default.display_options.fields.social_views_bulk_operations_bulk_form_enrollments_1.selected_actions');
  $selected_actions[] = [
    'action_id' => 'social_event_enrolments_export_enrollments_action',
    'preconfiguration' => [
      'label_override' => t('Export'),
    ],
  ];
  $config->set('display.default.display_options.fields.social_views_bulk_operations_bulk_form_enrollments_1.selected_actions', $selected_actions);
  $config->save();
}
