improvements-2.x-dev/modules/improvements_node/improvements_node.module

modules/improvements_node/improvements_node.module
<?php

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeForm;
use Drupal\node\NodeInterface;
use Drupal\node\NodeTypeForm;
use Drupal\node\NodeTypeInterface;

/**
 * Implements hook_form_BASE_FORM_ID_alter(): node_type_form.
 */
function improvements_node_form_node_type_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
  $node_type_form_object = $form_state->getFormObject(); /** @var NodeTypeForm $node_type_form_object */
  $node_type_entity = $node_type_form_object->getEntity(); /** @var NodeTypeInterface $node_type_entity */

  $form['submission']['hide_revision_group'] = [
    '#type' => 'checkbox',
    '#title' => t('Hide revision group'),
    '#default_value' => $node_type_entity->getThirdPartySetting('improvements', 'hide_revision_group'),
  ];

  $form['submission']['move_status_to_vertical_tab'] = [
    '#type' => 'checkbox',
    '#title' => t('Move "Status" field to vertical tab'),
    '#default_value' => $node_type_entity->getThirdPartySetting('improvements', 'move_status_to_vertical_tab'),
  ];

  if ($node_type_entity->isNew()) {
    $node_type_entity->setThirdPartySetting('menu_ui', 'available_menus', []);

    $form['submission']['preview_mode']['#default_value'] = 0;
    $form['workflow']['options']['#default_value']['promote'] = FALSE;
    $form['workflow']['options']['#default_value']['revision'] = FALSE;
    $form['display']['display_submitted']['#default_value'] = FALSE;
  }

  $form['#entity_builders'][] = 'improvements_node_type_form_entity_builder';
}

/**
 * Node type form entity builder callback.
 */
function improvements_node_type_form_entity_builder(string $entity_type, NodeTypeInterface $node_type, array &$form, FormStateInterface $form_state): void {
  $node_type->setThirdPartySetting('improvements', 'hide_revision_group', $form_state->getValue('hide_revision_group'));
  $node_type->setThirdPartySetting('improvements', 'move_status_to_vertical_tab', $form_state->getValue('move_status_to_vertical_tab'));
}

/**
 * Implements hook_form_BASE_FORM_ID_alter(): node_form.
 */
function improvements_node_form_node_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
  $form_object = $form_state->getFormObject(); /** @var NodeForm $form_object */
  $node = $form_object->getEntity(); /** @var NodeInterface $node */
  $node_type_entity = $node->get('type')->entity; /** @var NodeTypeInterface $node_type_entity */

  // Add autofocus to title field
  if ($node->isNew() && isset($form['title']['widget'][0]['value'])) {
    $form['title']['widget'][0]['value']['#attributes']['autofocus'] = 'autofocus';
  }

  // Hide revision group
  if (
    $node_type_entity->getThirdPartySetting('improvements', 'hide_revision_group') &&
    !$form['revision']['#default_value'] &&
    isset($form['revision_information'])
  ) {
    $form['revision_information']['#access'] = FALSE;
  }

  // Move status field to fieldset
  if ($node_type_entity->getThirdPartySetting('improvements', 'move_status_to_vertical_tab')) {
    $form['status']['#group'] = 'options';
    $form['options']['#title'] = t('Publishing options');
  }

  // Trigger hook_form_node_BUNDLE_form_alter for edit form
  // @TODO Add test
  if ($form_object->getOperation() == 'edit') {
    \Drupal::moduleHandler()->alter('form_node_' . $node->bundle() . '_form', $form, $form_state, $form_id);
  }
}

/**
 * Implements hook_entity_base_field_info_alter().
 *
 * @param BaseFieldDefinition[] $fields
 * @param EntityTypeInterface $entity_type
 *
 * @see Node::baseFieldDefinitions()
 */
function improvements_node_entity_base_field_info_alter(array &$fields, EntityTypeInterface $entity_type): void {
  if ($entity_type->id() == 'node') {
    $fields['title']->setDisplayConfigurable('view', TRUE);
    $fields['uid']->setDisplayConfigurable('view', TRUE); // @TODO Add test
    $fields['created']->setDisplayConfigurable('view', TRUE)->setLabel(t('Date of creation'));
    $fields['changed']->setLabel(t('Date of change'));
  }
}

/**
 * Implements hook_entity_type_build().
 */
function improvements_node_entity_type_build(array &$entity_types): void {
  // See https://www.drupal.org/node/3043840
  if (isset($entity_types['node'])) {
    $entity_types['node']->set('enable_base_field_custom_preprocess_skipping', TRUE);
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc