<?php

/**
 * @file
 * The Social book module.
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\node\NodeInterface;

/**
 * Implements hook_node_links_alter().
 */
function social_book_node_links_alter(array &$links, NodeInterface $entity, array &$context) {
  // Remove the printer-friendly version link.
  unset($links['book']['#links']['book_printer']);
  unset($links['book']['#links']['book_add_child']);
}

/**
 * Implements hook_menu_local_tasks_alter().
 */
function social_book_menu_local_tasks_alter(&$data, $route_name) {
  // Remove the outline from ALL pages.
  if (isset($data['tabs'][0]['entity.node.book_outline_form'])) {
    unset($data['tabs'][0]['entity.node.book_outline_form']);
  }
}

/**
 * Implements hook_social_user_account_header_create_links().
 *
 * Adds the "Create Book" link to the content creation menu if the user is
 * allowed to create new books.
 */
function social_book_social_user_account_header_create_links($context): array {
  $link = Link::createFromRoute((new TranslatableMarkup('New Book Page')), 'node.add', [
    'node_type' => 'book',
  ]);

  return [
    'add_book' => [
      '#type' => 'link',
      '#attributes' => [
        'title' => new TranslatableMarkup('Create New Book Page'),
      ],
      '#access' => $link->getUrl()->access(),
    ] + $link->toRenderable(),
  ];
}

/**
 * Implements hook_social_core_compatible_content_forms_alter().
 */
function social_book_social_core_compatible_content_forms_alter(&$compatible_content_type_forms) {
  $compatible_content_type_forms[] = 'node_book_form';
  $compatible_content_type_forms[] = 'node_book_edit_form';
}

/**
 * Implements hook_social_tagging_type_alter().
 */
function social_book_social_tagging_type_alter(array &$items): void {
  $items['node']['sets'][0]['bundles'][] = 'book';
}

/**
 * Implements hook_form_FORM_ID_alter() for "node_form".
 */
function social_book_form_node_form_alter(array &$form, FormStateInterface $form_state): void {
  /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
  $form_object = $form_state->getFormObject();
  /** @var \Drupal\node\NodeInterface $node */
  $node = $form_object->getEntity();

  if ($node->bundle() !== 'book') {
    return;
  }

  unset($form['book']['bid']['#options'][0]);
}
