123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- # @Author: Bachir Soussi Chiadmi <bach>
- # @Date: 13-12-2017
- # @Email: bachir@figureslibres.io
- # @Filename: edlp_corpus.routing.yml
- # @Last modified by: bach
- # @Last modified time: 20-12-2017
- # @License: GPL-V3
- use Drupal\Core\Entity\EntityInterface;
- use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
- use Drupal\Core\Url;
- use Drupal\workflow\Entity\WorkflowManager;
- /**
- * Implements hook_theme().
- */
- function edlp_corpus_theme($existing, $type, $theme, $path) {
- // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
- return array(
- 'blockentrees' => array(
- // 'render element' => '',
- 'file' => 'blockentrees.inc',
- 'variables' => array(
- 'entrees_items' => array(),
- ),
- ),
- );
- }
- /**
- * hook_entity_extra_field_info()
- */
- function edlp_corpus_entity_extra_field_info(){
- $extra = [];
- $extra['taxonomy_term']['entrees']['display']['index'] = [
- 'label' => t('Index'),
- 'description' => 'Display index of all documents tagued with the term',
- 'weight' => 99,
- // 'visible' => FALSE,
- ];
- return $extra;
- }
- /**
- * Implements hook_ENTITY_TYPE_view().
- * @see https://www.amazeelabs.com/en/render-menu-tree-custom-code-drupal-8
- */
- function edlp_corpus_taxonomy_term_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
- $index_display_settings = $display->getComponent('index');
- if (!empty($index_display_settings)) {
- // dpm($entity);
- // dpm($entity->id());
- $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
- $query = \Drupal::entityQuery('node')
- ->condition('status', 1)
- ->condition('type', 'enregistrement')
- ->condition('field_entrees', $entity->id());
- $documents_nids = $query->execute();
- $documents = entity_load_multiple('node', $documents_nids);
- $documents_list = array (
- '#theme' => 'item_list',
- '#items' => [],
- );
- foreach($documents as $doc){
- // remove masqué
- $sid = WorkflowManager::getCurrentStateId($doc, 'field_workflow');
- if($sid != 'corpus_documents_publie') continue;
- // TODO: instead of workflow, just check access
- $documents_list['#items'][] = $view_builder->view($doc, 'index');
- }
- // And the last step is to actually build the tree.
- $build['index'] = array(
- '#type'=>"container",
- '#attributes'=>array(
- 'class'=>'index'
- ),
- 'label'=>array(
- '#type' => 'container',
- "#markup"=> t("Index"),
- '#attributes'=>array(
- 'class'=>'field__label'
- ),
- ),
- 'documents'=> $documents_list
- );
- }
- }
- /**
- * Implements hook_page_attachments().
- * @param array $attachments
- */
- function edlp_corpus_page_attachments(array &$attachments) {
- // css class
- // :root{
- // --e-col-130:#4B8F7E;
- // }
- // javascript list
- // drupalSettings edlp_corpus{
- // "e_col_130": "rgb(75, 143, 126)",
- // }
- // get all entries
- $query = \Drupal::entityQuery('taxonomy_term')
- ->condition('vid', 'entrees');
- $tids = $query->execute();
- $terms = entity_load_multiple('taxonomy_term', $tids);
- $js_list = [];
- $css_str = ":root{\n";
- foreach ($terms as $term) {
- $tid = $term->id();
- // get the color value
- $color = $term->get('field_color')->color;
- // build colors list
- $css_str .= "\t".'--e-col-'.$tid.':'.$color.";\n";
- $js_list['e_col_'.$tid] = $color;
- }
- $css_str .= '}';
- // attache css
- // we should find a better way
- // https://www.drupal.org/docs/8/creating-custom-modules/adding-stylesheets-css-and-javascript-js-to-a-drupal-8-module
- $attachments['#attached']['html_head'][] = [
- [
- '#type' => 'html_tag',
- '#tag' => 'style',
- '#value' => $css_str,
- ],
- // A key, to make it possible to recognize this HTML element when altering.
- 'edlp_colors',
- ];
- $attachments['#attached']['drupalSettings']['edlp_corpus']['colors'] = $js_list;
- // load corpus
- $url = Url::fromRoute('edlp_corpus.corpusjson');
- $attachments['#attached']['drupalSettings']['edlp_corpus']['load_corpus_ajax_url'] = $url->getInternalPath();
- }
- function _edlp_corpus_invalidate_corpus_cache($node){
- if($node->getType() == 'enregistrement'){
- $sid = WorkflowManager::getCurrentStateId($node, 'field_workflow');
- if($sid == 'corpus_documents_publie'){
- \Drupal::service('cache_tags.invalidator')->invalidateTags(['rebuild-corpus-cache']);
- }
- }
- }
- /**
- * Acts when creating a new entity of a specific type.
- * This hook runs after a new entity object has just been instantiated.
- * @param \Drupal\Core\Entity\EntityInterface $entity
- * The entity object.
- * @ingroup entity_crud
- * @see hook_entity_create()
- */
- function edlp_corpus_node_create(EntityInterface $node) {
- _edlp_corpus_invalidate_corpus_cache($node);
- }
- /**
- * Respond to updates to an entity of a particular type.
- * This hook runs once the entity storage has been updated. Note that hook
- * implementations may not alter the stored entity data. Get the original entity
- * object from $entity->original.
- * @param \Drupal\Core\Entity\EntityInterface $entity
- * The entity object.
- * @ingroup entity_crud
- * @see hook_entity_update()
- */
- function edlp_corpus_node_update(EntityInterface $node) {
- _edlp_corpus_invalidate_corpus_cache($node);
- }
- /**
- * Respond to entity deletion.
- * This hook runs once the entity has been deleted from the storage.
- * @param \Drupal\Core\Entity\EntityInterface $entity
- * The entity object for the entity that has been deleted.
- * @ingroup entity_crud
- * @see hook_ENTITY_TYPE_delete()
- */
- function edlp_corpus_node_delete(EntityInterface $node) {
- _edlp_corpus_invalidate_corpus_cache($node);
- }
|