materio_home.module 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. * Implement hook_cron().
  129. *
  130. */
  131. function materio_home_cron(){
  132. $logger = \Drupal::logger('materio_home');
  133. // recompile every 5 minutes
  134. // $request_time = \Drupal::time()->getRequestTime();
  135. // if ($request_time - \Drupal::state()->get('materio_home.last_recompute') >= 300) {
  136. // Short-running operation example, not using a queue
  137. $query = \Drupal::entityQuery('node')
  138. ->condition('status', 1)
  139. ->condition('type', 'frontpage');
  140. $nids = $query->execute();
  141. $nodes = entity_load_multiple('node', $nids);
  142. foreach ($nodes as $nid => $node) {
  143. $logger->notice('cron | regenerating computed fields for '.$nid);
  144. $node->save();
  145. }
  146. // \Drupal::state()->set('materio_home.last_recompute', $request_time);
  147. // }
  148. }
  149. /**
  150. * Implements hook_theme().
  151. */
  152. // function materio_home_theme($existing, $type, $theme, $path) {
  153. // // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
  154. //
  155. // return array(
  156. // 'materio_home' => array(
  157. // // 'render element' => '',
  158. // 'file' => 'includes/materio_home.inc',
  159. // 'variables' => array(
  160. // 'frontpage_node' => NULL,
  161. // ),
  162. // ),
  163. // );
  164. // }
  165. // /**
  166. // * implements hook_entity_extra_field_info
  167. // *
  168. // */
  169. // function materio_home_entity_extra_field_info() {
  170. // $extra = [];
  171. // // $extra['node']['frontpage']['form']['computed_materials_reference'] = [
  172. // // 'label' => t('Computed Materials References'),
  173. // // 'description' => t('Computed Materials References'),
  174. // // 'weight' => 10,
  175. // // ];
  176. // // $extra['node']['frontpage']['display']['computed_materials_reference'] = [
  177. // // 'label' => t('Computed Materials References'),
  178. // // 'description' => t('Computed Materials References'),
  179. // // 'weight' => 10,
  180. // // ];
  181. // return $extra;
  182. // }
  183. //
  184. // /**
  185. // * Implements hook_install().
  186. // */
  187. // function materio_home_install() {
  188. // $entity_type = \Drupal::service('entity_type.manager')->getDefinition('node');
  189. // \Drupal::service('entity.definition_update_manager')->updateEntityType($entity_type);
  190. // }
  191. //
  192. // /**
  193. // * Implements hook_uninstall().
  194. // */
  195. // function materio_home_uninstall() {
  196. // $entity_type = \Drupal::service('entity_type.manager')->getDefinition('node');
  197. // \Drupal::service('entity.definition_update_manager')->updateEntityType($entity_type);
  198. // }