materio-d9/web/modules/custom/materio_home/materio_home.module

262 lines
9.6 KiB
Plaintext

<?php
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\mymodule\Plugin\Field\FieldType\MyFieldComputed;
// https://www.drupal.org/project/drupal/issues/2916266#comment-12301574
// $key_value_factory = \Drupal::service('keyvalue');
// $field_map_kv_store = $key_value_factory->get('entity.definitions.bundle_field_map');
// $node_map = $field_map_kv_store->get('node');
// unset($node_map['field_workflow']['bundles']['breve']);
// unset($node_map['field_visuel']['bundles']['breve']);
// unset($node_map['field_video']['bundles']['breve']);
// unset($node_map['field_thesaurus']['bundles']['breve']);
// unset($node_map['field_tags']['bundles']['breve']);
// unset($node_map['field_source']['bundles']['breve']);
// unset($node_map['field_showroom']['bundles']['breve']);
// unset($node_map['field_migration']['bundles']['breve']);
// unset($node_map['field_linked_materials']['bundles']['breve']);
// unset($node_map['field_memo']['bundles']['breve']);
// $field_map_kv_store->set('node', $node_map);
/**
* Implement hook_entity_bundle_field_info().
*
* @param EntityTypeInterface $entity_type
* @param $bundle
* @param array $base_field_definitions
* @return array
*/
function materio_home_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
// function materio_home_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
// $fields = array();
// if ($entity_type->id() == 'node' && $bundle === 'frontpage') {
// \Drupal::logger('materio_home')->notice('bundle: '.$bundle);
if ($entity_type->id() == 'node' && $bundle == 'frontpage') {
$fields['computed_materials_reference'] = BaseFieldDefinition::create('entity_reference')
->setName('computed_materials_reference')
->setLabel(t('Computed Materials References'))
->setDescription(t('Computed Materials References.'))
// // The Entity Type this field belongs to.
->setTargetEntityTypeId($entity_type->id())
// // The Entity Type bundle this field belongs to.
->setTargetBundle($bundle)
->setSetting('target_type', 'node')
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setComputed(TRUE)
->setRevisionable(FALSE)
->setTranslatable(FALSE)
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'weight' => -5,
])
->setClass(\Drupal\materio_home\Plugin\Field\FieldType\ComputedMaterialsReferences::class);
$fields['computed_showrooms_reference'] = BaseFieldDefinition::create('entity_reference')
->setName('computed_showrooms_reference')
->setLabel(t('Computed Showrooms References'))
->setDescription(t('Computed Showrooms References.'))
// // The Entity Type this field belongs to.
->setTargetEntityTypeId($entity_type->id())
// // The Entity Type bundle this field belongs to.
->setTargetBundle($bundle)
->setSetting('target_type', 'taxonomy_term')
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setComputed(TRUE)
->setRevisionable(FALSE)
->setTranslatable(FALSE)
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'weight' => -5,
])
->setClass(\Drupal\materio_home\Plugin\Field\FieldType\ComputedShowroomsReferences::class);
$fields['computed_articles_reference'] = BaseFieldDefinition::create('entity_reference')
->setName('computed_articles_reference')
->setLabel(t('Computed Articles References'))
->setDescription(t('Computed Articles References.'))
// // The Entity Type this field belongs to.
->setTargetEntityTypeId($entity_type->id())
// // The Entity Type bundle this field belongs to.
->setTargetBundle($bundle)
->setSetting('target_type', 'node')
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setComputed(TRUE)
->setRevisionable(FALSE)
->setTranslatable(FALSE)
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'weight' => -5,
])
->setClass(\Drupal\materio_home\Plugin\Field\FieldType\ComputedArticlesReferences::class);
$fields['computed_products_reference'] = BaseFieldDefinition::create('entity_reference')
->setName('computed_products_reference')
->setLabel(t('Computed Products References'))
->setDescription(t('Computed Products References.'))
// // The Entity Type this field belongs to.
->setTargetEntityTypeId($entity_type->id())
// // The Entity Type bundle this field belongs to.
->setTargetBundle($bundle)
->setSetting('target_type', 'commerce_product')
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setComputed(TRUE)
->setRevisionable(FALSE)
->setTranslatable(FALSE)
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'weight' => -5,
])
->setClass(\Drupal\materio_home\Plugin\Field\FieldType\ComputedCommerceProductReferences::class);
return $fields;
}
}
/**
* Implement hook_entity_base_field_info_alter().
*
* @param $fields
* @param EntityTypeInterface $entity_type
*/
function materio_home_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
if ($entity_type->id() == 'node') {
if (isset($fields['title'])) {
$fields['title']->setDisplayConfigurable('view', TRUE);
}
}
}
/**
* Implements hook_theme().
*
* Register a module or theme's theme implementations.
* The implementations declared by this hook specify how a particular render array is to be rendered as HTML.
*
* See: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21theme.api.php/function/hook_theme/8.2.x
*
* If you change this method, clear theme registry and routing table 'drush cc theme-registry' and 'drush cc router'.
*/
function materio_home_theme($existing, $type, $theme, $path) {
return [
// Name of the theme hook. This is used in the controller to trigger the hook.
'materio_home' => [
'render element' => 'element',
// If no template name is defined here, it defaults to the name of the theme hook, ie. module-name-theme-hook.html.twig
'template' => 'materio-home',
// Optionally define path to Twig template files. Defaults to the module's ./templates/ directory.
// 'path' => $path . '/templates',
// Optionally define variables that will be passed to the Twig template and set default values for them.
// 'variables' => [
// 'variable1' => 'Yet another default text.',
// 'variable2' => 0,
// 'variable3' => [0, 0, 0],
// ],
],
];
}
function template_preprocess_materio_home(&$vars){
$node_view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
$element = $vars['element'];
$vm = $element['#view_mode'];
$fpnode = $element['#frontpage_node'];
$nvb_fpnode = $node_view_builder->view($fpnode, $vm);
$vars['frontpage_node'] = $nvb_fpnode;
}
/**
* Implement hook_cron().
*
*/
function materio_home_cron(){
$logger = \Drupal::logger('materio_home');
// recompile every 5 minutes
// $request_time = \Drupal::time()->getRequestTime();
// if ($request_time - \Drupal::state()->get('materio_home.last_recompute') >= 300) {
// Short-running operation example, not using a queue
$query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'frontpage');
$nids = $query->execute();
$nodes = entity_load_multiple('node', $nids);
foreach ($nodes as $nid => $node) {
$logger->notice('cron | regenerating computed fields for '.$nid);
$node->save();
}
// \Drupal::state()->set('materio_home.last_recompute', $request_time);
// }
}
/**
* Implements hook_theme().
*/
// function materio_home_theme($existing, $type, $theme, $path) {
// // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
//
// return array(
// 'materio_home' => array(
// // 'render element' => '',
// 'file' => 'includes/materio_home.inc',
// 'variables' => array(
// 'frontpage_node' => NULL,
// ),
// ),
// );
// }
// /**
// * implements hook_entity_extra_field_info
// *
// */
// function materio_home_entity_extra_field_info() {
// $extra = [];
// // $extra['node']['frontpage']['form']['computed_materials_reference'] = [
// // 'label' => t('Computed Materials References'),
// // 'description' => t('Computed Materials References'),
// // 'weight' => 10,
// // ];
// // $extra['node']['frontpage']['display']['computed_materials_reference'] = [
// // 'label' => t('Computed Materials References'),
// // 'description' => t('Computed Materials References'),
// // 'weight' => 10,
// // ];
// return $extra;
// }
//
// /**
// * Implements hook_install().
// */
// function materio_home_install() {
// $entity_type = \Drupal::service('entity_type.manager')->getDefinition('node');
// \Drupal::service('entity.definition_update_manager')->updateEntityType($entity_type);
// }
//
// /**
// * Implements hook_uninstall().
// */
// function materio_home_uninstall() {
// $entity_type = \Drupal::service('entity_type.manager')->getDefinition('node');
// \Drupal::service('entity.definition_update_manager')->updateEntityType($entity_type);
// }