<?php

/**
 * @file
 * Install, update, and uninstall functions for d_mautic.install.
 */

use Drupal\field\Entity\FieldConfig;

/**
 * Implements hook_install().
 */
function d_mautic_install() {
  Drupal::service('d_update')
    ->updateConfigurations('module/d_mautic', 'd_mautic.init');
}

/**
 * Implements hook_uninstall().
 */
function d_mautic_uninstall() {
  // Deletes node references to d_mautic paragraph after uninstalling module.
  $ids = [];
  $allParagraphs = \Drupal::entityTypeManager()->getStorage('paragraph')->loadMultiple();

  foreach ($allParagraphs as $paragraph) {
    $type = $paragraph->bundle();
    if ($type === 'd_mautic') {
      $ids[] = $paragraph->get('id')->getValue()[0]['value'];
    }
  }

  $allNodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple();
  foreach ($allNodes as $node) {
    if ($node->hasField('field_page_section')) {
      $page_section = $node->get('field_page_section')->getValue();
      foreach ($page_section as $index => $section) {
        if (in_array($section['target_id'], $ids)) {
          unset($page_section[$index]);
          $node->field_page_section = $page_section;
          $node->save();
        }
      }
    }
  }

  // Updates field configuration.
  $field_instance = FieldConfig::loadByName('node', 'content_page', 'field_page_section');
  $settings = $field_instance->getSetting('handler_settings');
  unset($settings['target_bundles_drag_drop']['d_mautic']);
  unset($settings['target_bundles']['d_mautic']);
  $field_instance->setSetting('handler_settings', $settings);
  $field_instance->save();
}
