domain_entity_type-1.0.0-rc2/modules/det_node/det_node.module
modules/det_node/det_node.module
<?php
/**
* @file
* Module related hooks.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeTypeInterface;
/**
* Implements hook_form_alter().
*/
function det_node_form_alter(&$form, FormStateInterface $form_state, $form_id): void {
$form_ids = [
'node_type_add_form',
'node_type_edit_form',
];
if (!in_array($form_id, $form_ids, TRUE)) {
return;
}
/** @var \Drupal\node\NodeTypeInterface|NULL $entity */
$entity = $form_state->getFormObject()->getEntity();
$domains = Drupal::entityTypeManager()->getStorage('domain')
->loadByProperties(['status' => 1]);
$domain_options = array_map(fn($domain) => $domain->label(), $domains);
$form['det_node'] = [
'#type' => 'details',
'#title' => t('Domain access'),
'#group' => 'additional_settings',
];
$form['det_node']['domains'] = [
'#type' => 'checkboxes',
'#title' => t('Domain access'),
'#options' => $domain_options,
'#default_value' => $entity ? $entity->getThirdPartySetting('det_node', 'domains', []) : [],
'#description' => t('Select the affiliate domain(s). If nothing was selected: Affiliated to all domains.'),
];
$form['#entity_builders'][] = 'det_node_content_type_builder';
}
/**
* Content type entity builder.
*/
function det_node_content_type_builder($entity_type, NodeTypeInterface $entity, &$form, FormStateInterface $form_state): void {
$domains = array_filter($form_state->getValue('domains'));
$entity->setThirdPartySetting('det_node', 'domains', $domains);
}
/**
* Implements hook_entity_type_build().
*/
function det_node_entity_type_build(array &$entity_types) {
if (isset($entity_types['node_type'])) {
$entity_types['node_type']->setAccessClass('Drupal\det_node\NodeTypeAccessOverride');
$entity_types['node_type']->setListBuilderClass('Drupal\det_node\NodeTypeListBuilderOverride');
}
}
