materio_home.module 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. use Drupal\Core\Entity\EntityTypeInterface;
  3. use Drupal\Core\Field\BaseFieldDefinition;
  4. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  5. use Drupal\mymodule\Plugin\Field\FieldType\MyFieldComputed;
  6. // https://www.drupal.org/project/drupal/issues/2916266#comment-12301574
  7. // $key_value_factory = \Drupal::service('keyvalue');
  8. // $field_map_kv_store = $key_value_factory->get('entity.definitions.bundle_field_map');
  9. // $node_map = $field_map_kv_store->get('node');
  10. // unset($node_map['field_workflow']['bundles']['breve']);
  11. // unset($node_map['field_visuel']['bundles']['breve']);
  12. // unset($node_map['field_video']['bundles']['breve']);
  13. // unset($node_map['field_thesaurus']['bundles']['breve']);
  14. // unset($node_map['field_tags']['bundles']['breve']);
  15. // unset($node_map['field_source']['bundles']['breve']);
  16. // unset($node_map['field_showroom']['bundles']['breve']);
  17. // unset($node_map['field_migration']['bundles']['breve']);
  18. // unset($node_map['field_linked_materials']['bundles']['breve']);
  19. // unset($node_map['field_memo']['bundles']['breve']);
  20. // $field_map_kv_store->set('node', $node_map);
  21. /**
  22. * Implement hook_entity_bundle_field_info().
  23. *
  24. * @param EntityTypeInterface $entity_type
  25. * @param $bundle
  26. * @param array $base_field_definitions
  27. * @return array
  28. */
  29. function materio_home_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
  30. // function materio_home_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  31. // $fields = array();
  32. // if ($entity_type->id() == 'node' && $bundle === 'frontpage') {
  33. // \Drupal::logger('materio_home')->notice('bundle: '.$bundle);
  34. if ($entity_type->id() == 'node' && $bundle == 'frontpage') {
  35. $fields['computed_materials_reference'] = BaseFieldDefinition::create('entity_reference')
  36. ->setName('computed_materials_reference')
  37. ->setLabel(t('Computed Materials References'))
  38. ->setDescription(t('Computed Materials References.'))
  39. // // The Entity Type this field belongs to.
  40. ->setTargetEntityTypeId($entity_type->id())
  41. // // The Entity Type bundle this field belongs to.
  42. ->setTargetBundle($bundle)
  43. ->setSetting('target_type', 'node')
  44. ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
  45. ->setComputed(TRUE)
  46. ->setRevisionable(FALSE)
  47. ->setTranslatable(FALSE)
  48. ->setDisplayConfigurable('view', TRUE)
  49. ->setDisplayOptions('view', [
  50. 'label' => 'hidden',
  51. 'weight' => -5,
  52. ])
  53. ->setClass(\Drupal\materio_home\Plugin\Field\FieldType\ComputedMaterialsReferences::class);
  54. $fields['computed_showrooms_reference'] = BaseFieldDefinition::create('entity_reference')
  55. ->setName('computed_showrooms_reference')
  56. ->setLabel(t('Computed Showrooms References'))
  57. ->setDescription(t('Computed Showrooms References.'))
  58. // // The Entity Type this field belongs to.
  59. ->setTargetEntityTypeId($entity_type->id())
  60. // // The Entity Type bundle this field belongs to.
  61. ->setTargetBundle($bundle)
  62. ->setSetting('target_type', 'taxonomy_term')
  63. ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
  64. ->setComputed(TRUE)
  65. ->setRevisionable(FALSE)
  66. ->setTranslatable(FALSE)
  67. ->setDisplayConfigurable('view', TRUE)
  68. ->setDisplayOptions('view', [
  69. 'label' => 'hidden',
  70. 'weight' => -5,
  71. ])
  72. ->setClass(\Drupal\materio_home\Plugin\Field\FieldType\ComputedShowroomsReferences::class);
  73. $fields['computed_articles_reference'] = BaseFieldDefinition::create('entity_reference')
  74. ->setName('computed_articles_reference')
  75. ->setLabel(t('Computed Articles References'))
  76. ->setDescription(t('Computed Articles References.'))
  77. // // The Entity Type this field belongs to.
  78. ->setTargetEntityTypeId($entity_type->id())
  79. // // The Entity Type bundle this field belongs to.
  80. ->setTargetBundle($bundle)
  81. ->setSetting('target_type', 'node')
  82. ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
  83. ->setComputed(TRUE)
  84. ->setRevisionable(FALSE)
  85. ->setTranslatable(FALSE)
  86. ->setDisplayConfigurable('view', TRUE)
  87. ->setDisplayOptions('view', [
  88. 'label' => 'hidden',
  89. 'weight' => -5,
  90. ])
  91. ->setClass(\Drupal\materio_home\Plugin\Field\FieldType\ComputedArticlesReferences::class);
  92. $fields['computed_products_reference'] = BaseFieldDefinition::create('entity_reference')
  93. ->setName('computed_products_reference')
  94. ->setLabel(t('Computed Products References'))
  95. ->setDescription(t('Computed Products References.'))
  96. // // The Entity Type this field belongs to.
  97. ->setTargetEntityTypeId($entity_type->id())
  98. // // The Entity Type bundle this field belongs to.
  99. ->setTargetBundle($bundle)
  100. ->setSetting('target_type', 'commerce_product')
  101. ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
  102. ->setComputed(TRUE)
  103. ->setRevisionable(FALSE)
  104. ->setTranslatable(FALSE)
  105. ->setDisplayConfigurable('view', TRUE)
  106. ->setDisplayOptions('view', [
  107. 'label' => 'hidden',
  108. 'weight' => -5,
  109. ])
  110. ->setClass(\Drupal\materio_home\Plugin\Field\FieldType\ComputedCommerceProductReferences::class);
  111. return $fields;
  112. }
  113. }
  114. /**
  115. * Implement hook_entity_base_field_info_alter().
  116. *
  117. * @param $fields
  118. * @param EntityTypeInterface $entity_type
  119. */
  120. function materio_home_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  121. if ($entity_type->id() == 'node') {
  122. if (isset($fields['title'])) {
  123. $fields['title']->setDisplayConfigurable('view', TRUE);
  124. }
  125. }
  126. }
  127. /**
  128. * Implements hook_theme().
  129. *
  130. * Register a module or theme's theme implementations.
  131. * The implementations declared by this hook specify how a particular render array is to be rendered as HTML.
  132. *
  133. * See: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21theme.api.php/function/hook_theme/8.2.x
  134. *
  135. * If you change this method, clear theme registry and routing table 'drush cc theme-registry' and 'drush cc router'.
  136. */
  137. function materio_home_theme($existing, $type, $theme, $path) {
  138. return [
  139. // Name of the theme hook. This is used in the controller to trigger the hook.
  140. 'materio_home' => [
  141. 'render element' => 'element',
  142. // If no template name is defined here, it defaults to the name of the theme hook, ie. module-name-theme-hook.html.twig
  143. 'template' => 'materio-home',
  144. // Optionally define path to Twig template files. Defaults to the module's ./templates/ directory.
  145. // 'path' => $path . '/templates',
  146. // Optionally define variables that will be passed to the Twig template and set default values for them.
  147. // 'variables' => [
  148. // 'variable1' => 'Yet another default text.',
  149. // 'variable2' => 0,
  150. // 'variable3' => [0, 0, 0],
  151. // ],
  152. ],
  153. ];
  154. }
  155. function template_preprocess_materio_home(&$vars){
  156. $node_view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
  157. $element = $vars['element'];
  158. $vm = $element['#view_mode'];
  159. $fpnode = $element['#frontpage_node'];
  160. $nvb_fpnode = $node_view_builder->view($fpnode, $vm);
  161. $vars['frontpage_node'] = $nvb_fpnode;
  162. }
  163. /**
  164. * Implement hook_cron().
  165. *
  166. */
  167. function materio_home_cron(){
  168. $logger = \Drupal::logger('materio_home');
  169. // recompile every 5 minutes
  170. // $request_time = \Drupal::time()->getRequestTime();
  171. // if ($request_time - \Drupal::state()->get('materio_home.last_recompute') >= 300) {
  172. // Short-running operation example, not using a queue
  173. $query = \Drupal::entityQuery('node')
  174. ->condition('status', 1)
  175. ->condition('type', 'frontpage');
  176. $nids = $query->execute();
  177. $nodes = entity_load_multiple('node', $nids);
  178. foreach ($nodes as $nid => $node) {
  179. $logger->notice('cron | regenerating computed fields for '.$nid);
  180. $node->save();
  181. }
  182. // \Drupal::state()->set('materio_home.last_recompute', $request_time);
  183. // }
  184. }
  185. /**
  186. * Implements hook_theme().
  187. */
  188. // function materio_home_theme($existing, $type, $theme, $path) {
  189. // // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
  190. //
  191. // return array(
  192. // 'materio_home' => array(
  193. // // 'render element' => '',
  194. // 'file' => 'includes/materio_home.inc',
  195. // 'variables' => array(
  196. // 'frontpage_node' => NULL,
  197. // ),
  198. // ),
  199. // );
  200. // }
  201. // /**
  202. // * implements hook_entity_extra_field_info
  203. // *
  204. // */
  205. // function materio_home_entity_extra_field_info() {
  206. // $extra = [];
  207. // // $extra['node']['frontpage']['form']['computed_materials_reference'] = [
  208. // // 'label' => t('Computed Materials References'),
  209. // // 'description' => t('Computed Materials References'),
  210. // // 'weight' => 10,
  211. // // ];
  212. // // $extra['node']['frontpage']['display']['computed_materials_reference'] = [
  213. // // 'label' => t('Computed Materials References'),
  214. // // 'description' => t('Computed Materials References'),
  215. // // 'weight' => 10,
  216. // // ];
  217. // return $extra;
  218. // }
  219. //
  220. // /**
  221. // * Implements hook_install().
  222. // */
  223. // function materio_home_install() {
  224. // $entity_type = \Drupal::service('entity_type.manager')->getDefinition('node');
  225. // \Drupal::service('entity.definition_update_manager')->updateEntityType($entity_type);
  226. // }
  227. //
  228. // /**
  229. // * Implements hook_uninstall().
  230. // */
  231. // function materio_home_uninstall() {
  232. // $entity_type = \Drupal::service('entity_type.manager')->getDefinition('node');
  233. // \Drupal::service('entity.definition_update_manager')->updateEntityType($entity_type);
  234. // }