materio_home.module 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. return $fields;
  93. }
  94. }
  95. /**
  96. * Implement hook_entity_base_field_info_alter().
  97. *
  98. * @param $fields
  99. * @param EntityTypeInterface $entity_type
  100. */
  101. function materio_home_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  102. if ($entity_type->id() == 'node') {
  103. if (isset($fields['title'])) {
  104. $fields['title']->setDisplayConfigurable('view', TRUE);
  105. }
  106. }
  107. }
  108. /**
  109. * Implement hook_cron().
  110. *
  111. */
  112. function materio_home_cron(){
  113. $logger = \Drupal::logger('materio_home');
  114. // recompile every 5 minutes
  115. // $request_time = \Drupal::time()->getRequestTime();
  116. // if ($request_time - \Drupal::state()->get('materio_home.last_recompute') >= 300) {
  117. // Short-running operation example, not using a queue
  118. $query = \Drupal::entityQuery('node')
  119. ->condition('status', 1)
  120. ->condition('type', 'frontpage');
  121. $nids = $query->execute();
  122. $nodes = entity_load_multiple('node', $nids);
  123. foreach ($nodes as $nid => $node) {
  124. $logger->notice('cron | regenerating computed fields for '.$nid);
  125. $node->save();
  126. }
  127. // \Drupal::state()->set('materio_home.last_recompute', $request_time);
  128. // }
  129. }
  130. /**
  131. * Implements hook_theme().
  132. */
  133. // function materio_home_theme($existing, $type, $theme, $path) {
  134. // // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
  135. //
  136. // return array(
  137. // 'materio_home' => array(
  138. // // 'render element' => '',
  139. // 'file' => 'includes/materio_home.inc',
  140. // 'variables' => array(
  141. // 'frontpage_node' => NULL,
  142. // ),
  143. // ),
  144. // );
  145. // }
  146. // /**
  147. // * implements hook_entity_extra_field_info
  148. // *
  149. // */
  150. // function materio_home_entity_extra_field_info() {
  151. // $extra = [];
  152. // // $extra['node']['frontpage']['form']['computed_materials_reference'] = [
  153. // // 'label' => t('Computed Materials References'),
  154. // // 'description' => t('Computed Materials References'),
  155. // // 'weight' => 10,
  156. // // ];
  157. // // $extra['node']['frontpage']['display']['computed_materials_reference'] = [
  158. // // 'label' => t('Computed Materials References'),
  159. // // 'description' => t('Computed Materials References'),
  160. // // 'weight' => 10,
  161. // // ];
  162. // return $extra;
  163. // }
  164. //
  165. // /**
  166. // * Implements hook_install().
  167. // */
  168. // function materio_home_install() {
  169. // $entity_type = \Drupal::service('entity_type.manager')->getDefinition('node');
  170. // \Drupal::service('entity.definition_update_manager')->updateEntityType($entity_type);
  171. // }
  172. //
  173. // /**
  174. // * Implements hook_uninstall().
  175. // */
  176. // function materio_home_uninstall() {
  177. // $entity_type = \Drupal::service('entity_type.manager')->getDefinition('node');
  178. // \Drupal::service('entity.definition_update_manager')->updateEntityType($entity_type);
  179. // }