<?php

/**
 * @file
 * Contains wxt_ext_page.install.
 */

use Drupal\Core\Config\FileStorage;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;

/**
 * Implements hook_install().
 */
function wxt_ext_page_install() {
  // Stop here during a config sync.
  if (Drupal::isConfigSyncing()) {
    return;
  }

  $modules = [
    'layout_library',
    'menu_ui',
    'wxt_ext_workflow',
  ];

  $modules = array_filter($modules, [Drupal::moduleHandler(), 'moduleExists']);
  if ($modules) {
    wxt_ext_page_modules_installed($modules);
  }

  // Enable translation for our core entities/bundles.
  $entities = [
    'node' => [
      'page',
    ],
  ];
  wxt_translation_enable_translation($entities);
}

/**
 * Add field_lead_title to page content type.
 */
function wxt_ext_page_update_8500() {
  $config_directory = new FileStorage(\Drupal::service('extension.list.module')->getPath('wxt_ext_page') . '/config/optional/');

  $entitytype_fields = [
    'node' => [
      'page' => [
        'field_lead_title',
      ],
    ],
  ];

  // Approach taken from https://thinkshout.com/blog/2020/11/populating-fields
  foreach ($entitytype_fields as $entitytype => $bundles) {
    foreach ($bundles as $bundle => $fields) {
      foreach ($fields as $field) {
        $field_storage_name = 'field.storage.' . $entitytype . '.' . $field;
        $config_record = $config_directory->read($field_storage_name);

        if (!FieldStorageConfig::loadByName($config_record['entity_type'], $config_record['field_name'])) {
          FieldStorageConfig::create($config_record)->save();
        }

        $field_config_name = 'field.field.' . $entitytype . '.' . $bundle . '.' . $field;
        $config_record = $config_directory->read($field_config_name);

        if (!FieldConfig::loadByName($config_record['entity_type'], $config_record['bundle'], $config_record['field_name'])) {
          FieldConfig::create($config_record)->save();
        }
      }
    }
  }
}
