# @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\Core\Link; 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' => 'includes/blockentrees.inc', 'variables' => array( 'entrees_items' => array(), ), ), 'edlp_corpus_lastdocs' => array( // 'render element' => '', 'file' => 'includes/edlp_corpus_lastdocs.inc', 'variables' => array( 'lastdocs_nodes' => NULL, ), ), 'edlp_corpus_articlesindex' => array( // 'render element' => '', 'file' => 'includes/edlp_corpus_articlesindex.inc', 'variables' => array( 'articles_nodes' => NULL, ), ), 'edlp_corpus_collection' => array( // 'render element' => '', 'file' => 'includes/edlp_corpus_collection.inc', 'variables' => array( 'entrees_terms' => NULL, ), ), ); } /** * 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, ]; $extra['taxonomy_term']['entrees']['display']['index-home'] = [ 'label' => t('Index Home'), 'description' => 'Display index of all documents tagued with the term (form home mobile)', 'weight' => 99, // 'visible' => FALSE, ]; $extra['node']['enregistrement']['display']['relations'] = [ 'label' => t('Relations'), 'description' => 'Display enregistrement relations with other content types', 'weight' => 99, ]; 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'); $index_home_display_settings = $display->getComponent('index-home'); if (!empty($index_display_settings) || !empty($index_home_display_settings)) { // dpm($entity); // dpm($entity->id()); $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node'); $query = \Drupal::entityQuery('node') ->condition('status', 1) ->condition('type', 'enregistrement') ->sort('title') ->condition('field_entrees', $entity->id()); $documents_nids = $query->execute(); $documents = entity_load_multiple('node', $documents_nids); $documents_list = array ( '#theme' => 'item_list', '#items' => [], ); if(!empty($index_home_display_settings)){ $view_mode = 'index_home'; }else{ $view_mode = 'index'; } 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 // TODO: get the view mode of document nodes from field settings $documents_list['#items'][] = $view_builder->view($doc, $view_mode); } // 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 ); } } function edlp_corpus_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) { if($entity->bundle() == "enregistrement"){ $relations_display_settings = $display->getComponent('relations'); if(!empty($relations_display_settings)){ // querying all productions page that have this document in their entity reference "field_documents_liés" $query = \Drupal::entityQuery('node') ->condition('status', 1) ->condition('type', 'page') // page (production) ->exists('field_page_type') ->condition('field_documents_lies.target_id', $entity->id(), 'IN'); $nids = $query->execute(); $nodes = entity_load_multiple('node', $nids); // dpm($nodes, '$nodes'); // build the array of relateds classified by page_type (taxonomy) $relateds = array(); foreach ($nodes as $nid => $node) { // $page_type = $node->get('field_page_type')->get(0)->getValue(); // dpm($page_type, 'page_type'); // $term = entity_load('taxonomy_term', $page_type['target_id']); // dpm($term, 'term'); // $relateds[$term->getName()][] = $node->getTitle(); $url = Url::fromRoute('entity.node.canonical', ['node'=>$node->id()]); $url->setOptions(array( 'attributes' => array( 'class' => ['ajax-link'], 'data-drupal-link-system-path' => $url->getInternalPath() ) )); $link = Link::fromTextAndUrl(trim($node->getTitle()), $url); // $relateds[$term->getName()][] = $link; $relateds[] = $link->toRenderable(); } // dpm($relateds, 'relateds'); // if relateds, build the sentence for display and the render array if(count($relateds)){ // $relations = '

' . t('This sound is about :') . '

'; // foreach ($relateds as $cat => $titles) { // // $relations .= '' . $cat . ' : ' . implode(', ', $titles) . ' | '; // $relations[] = // } // $relations = preg_replace('/\s\|\s$/', '', $relations); // $relations .= '

'; $build['relations'] = array( '#type'=>"container", '#attributes'=>array( 'class'=>'relations' ), 'title' => array( "#markup"=> '

' . t('This sound is about :') . '

', ), 'content' => array ( '#theme' => 'item_list', '#items' => $relateds, ), ); } } } } /** * 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(); // load articles $url = Url::fromRoute('edlp_corpus.articlesindex'); $attachments['#attached']['drupalSettings']['edlp_corpus']['articlesindex_url'] = $url->getInternalPath(); // random btn link title $attachments['#attached']['drupalSettings']['edlp_corpus']['random_link_title'] = t('Shuffle the selection'); } 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); }