<?php

/**
 * @file
 * Thunder IVW install hooks.
 */

/**
 * Implements hook_install().
 */
function thunder_ivw_install(): void {

  $fieldWidget = 'ivw_integration_widget';

  // Attach field if channel vocabulary and article node type is
  // present in the distribution.
  foreach (['article', 'news_article'] as $type) {
    try {
      \Drupal::service('entity_display.repository')
        ->getFormDisplay('node', $type, 'default')
        ->setComponent(
          'field_ivw',
          ['type' => $fieldWidget]
        )->save();
    }
    catch (Exception $e) {
      \Drupal::logger('thunder')
        ->info(t('Could not add ivw field to @type node: "@message"',
            [
              '@type' => $type,
              '@message' => $e->getMessage(),
            ])
        );
    }
  }

  try {
    \Drupal::service('entity_display.repository')
      ->getFormDisplay('taxonomy_term', 'channel', 'default')
      ->setComponent('field_ivw',
        ['type' => $fieldWidget]
      )->save();
  }
  catch (Exception $e) {
    \Drupal::logger('thunder')->info(t('Could not add ivw field to channel taxonomy: "@message"', ['@message' => $e->getMessage()]));
  }

}

/* hook_update_n implementations should be in the profile instead of this
submodule. */
