<?php

/**
 * @file
 * The Social post photo installation.
 */

use Drupal\Core\Entity\Entity\EntityViewDisplay;

/**
 * Install hook for Social Post Photo.
 */
function social_post_photo_install() {
  // Grant the default permissions for this feature.
  user_role_grant_permissions(
    'verified',
    [
      'add photo post entities',
    ]
  );
  user_role_grant_permissions(
    'contentmanager',
    [
      'add photo post entities',
    ]
  );
  user_role_grant_permissions(
    'sitemanager',
    [
      'add photo post entities',
    ]
  );

  // Change post to photo.
  _social_post_photo_settype('post', 'photo');
}

/**
 * Uninstall hook for Social Post Photo.
 */
function social_post_photo_uninstall() {

  // Change photo to post.
  _social_post_photo_settype('photo', 'post');

  $configs = [
    'core.entity_form_display.post.photo.default',
    'core.entity_form_display.post.photo.group',
    'core.entity_form_display.post.photo.profile',
    'core.entity_view_display.post.photo.activity',
    'core.entity_view_display.post.photo.activity_comment',
    'core.entity_view_display.post.photo.default',
    'field.field.post.photo.field_post',
    'field.field.post.photo.field_post_comments',
    'field.field.post.photo.field_post_image',
    'field.field.post.photo.field_recipient_group',
    'field.field.post.photo.field_recipient_user',
    'field.field.post.photo.field_visibility',
    'field.storage.post.field_post_image',
    'image.style.social_post_photo',
    'social_post.post_type.photo',
  ];

  foreach ($configs as $config) {
    // Deleting config.
    \Drupal::configFactory()->getEditable($config)->delete();
  }
}

/**
 * Set the number of comments on Post Teaser and Full display.
 */
function social_post_photo_update_8001() {
  \Drupal::service('config.installer')->installDefaultConfig('module', 'social_post_photo');
}

/**
 * Create "Featured" view display for photo post.
 */
function social_post_photo_update_8002() {
  if (!EntityViewDisplay::load('post.photo.featured')) {
    $display = EntityViewDisplay::load('post.photo.default')->toArray();
    $display = array_merge($display, [
      'uuid' => NULL,
      '_core' => NULL,
      'targetEntityType' => 'post',
      'mode' => 'featured',
    ]);
    EntityViewDisplay::create($display)->save();
  }
}

/**
 * Change placeholder text of the post text field.
 */
function social_post_photo_update_8901() {
  /** @var \Drupal\update_helper\UpdaterInterface $update_helper */
  $update_helper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $update_helper->executeUpdate('social_post_photo', 'social_post_photo_update_8901');

  // Output logged messages to related channel of update execution.
  return $update_helper->logger()->output();
}

/**
 * Display field_album for post photo featured view mode.
 */
function social_post_photo_update_10201() {
  $config = \Drupal::configFactory()->getEditable('core.entity_view_display.post.photo.featured');
  $content = $config->get('content');
  $hidden = $config->get('hidden');

  $content['field_album'] = [
    'type' => 'social_album_entity_reference_label',
    'weight' => 4,
    'region' => 'content',
    'label' => 'hidden',
    'settings' => ['link' => TRUE],
    'third_party_settings' => [],
  ];

  if (in_array('field_album', $hidden)) {
    unset($hidden['field_album']);
  }

  $config
    ->set('content', $content)
    ->set('hidden', $hidden)
    ->save();
}

/**
 * Remove the field_album from entity view display.
 */
function social_post_photo_update_12001(): string {
  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper->executeUpdate('social_post_photo', __FUNCTION__);

  // Output logged messages to related channel of update execution.
  return $updateHelper->logger()->output();
}
