Mercurial > defr > drupal > noderef_view
comparison noderef_view.module @ 0:a3c1e224e807
Import initial du module noderef_view
| author | Franck Deroche <franck@defr.org> |
|---|---|
| date | Fri, 23 Jan 2009 14:52:03 +0100 |
| parents | |
| children | 71a07534c366 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a3c1e224e807 |
|---|---|
| 1 <?php | |
| 2 // vim: set ts=2 sw=2 expandtab syntax=php: | |
| 3 | |
| 4 /** | |
| 5 * @file | |
| 6 * Fichier principale du module noderef_view | |
| 7 * | |
| 8 * Ce module permet d'ajouter à tous les champs de type "Node Reference" | |
| 9 * pour lesquels une view définissant les nodes autorisés a été définie | |
| 10 * un lien 'Search' affichant la view dans une popup (via le module Popups API), | |
| 11 * permettant de choisir les nodes à mettre dans le champ en disposant de la | |
| 12 * puissance des Views pour la recherche (Exposed Filters notamment). | |
| 13 */ | |
| 14 | |
| 15 /** | |
| 16 * Implémentation de hook_view_action_info(). | |
| 17 * | |
| 18 * On crée une nouvelle action, nommée Select, et qui sera utilisée pour marquer | |
| 19 * les nodes comme données à utiliser. L'utilisation concrète qui en est faite | |
| 20 * dans ce module consiste à utiliser ces nœuds selectionés pour les ajouter | |
| 21 * dans le champ Node Reference étant à l'origine de la recherche. | |
| 22 */ | |
| 23 function noderef_view_action_info() { | |
| 24 return array( | |
| 25 'noderef_view_select' => array( | |
| 26 'description' => t('Select for later use'), | |
| 27 'type' => 'node', | |
| 28 'configurable' => FALSE, | |
| 29 'hooks' => array( | |
| 30 'nodeapi' => array() | |
| 31 ) | |
| 32 ) | |
| 33 ); | |
| 34 } | |
| 35 | |
| 36 /** | |
| 37 * Implémentation concrète de l'action de selection. | |
| 38 * | |
| 39 * On vide toute eventuelle selection précédente, puis on passe dans le cookie | |
| 40 * les informations sur la selection qui a été faite. | |
| 41 */ | |
| 42 function noderef_view_select($node, $context) { | |
| 43 static $count = 0; | |
| 44 if ($count == 0 && is_array($_COOKIE['noderef'])) { | |
| 45 // Reset the cookies | |
| 46 $before = time() - 300; | |
| 47 foreach($_COOKIE['noderef'] as $id => $val) { | |
| 48 setcookie('noderef['. $id .']', 0, $before, '/'); | |
| 49 } | |
| 50 } | |
| 51 $count++; | |
| 52 $five = time() + 300; | |
| 53 setrawcookie('noderef['. $node->nid .']', rawurlencode($node->title), $five, '/'); | |
| 54 dsm(t('!title added to selection', array('!title' => $node->title))); | |
| 55 } | |
| 56 | |
| 57 /** | |
| 58 * Implémentation de hook_form_alter(). | |
| 59 * | |
| 60 * On parcoure tous les formulaires à la recherche d'éventuels champs | |
| 61 * de type "Node Reference" sur lesquels appliqués nos transformations. | |
| 62 */ | |
| 63 function noderef_view_form_alter(&$form, $form_state, $form_id) { | |
| 64 if (isset($form['type']) && $form_id == $form['type']['#value'] .'_node_form') { | |
| 65 // That's a node form, get the fields and alter the form if needed | |
| 66 $fields = content_fields(); | |
| 67 $alter = _noderef_view_walk_form($form, $fields); | |
| 68 if ($alter) { | |
| 69 // We've changed the form, add the JS-behavior and Views CSS | |
| 70 // (the latter to deal with exposed filters in the popup) | |
| 71 drupal_add_css(drupal_get_path('module', 'views') .'/css/views.css'); | |
| 72 drupal_add_js(drupal_get_path('module', 'noderef_view') .'/noderef_view.js'); | |
| 73 } | |
| 74 } | |
| 75 elseif ($form_id == 'views_exposed_form' && isset($_GET['destination'])) { | |
| 76 // Views "Exposed Filter" forms doesn't preserve destination by default. | |
| 77 $form['destination'] = array( | |
| 78 '#type' => 'hidden', | |
| 79 '#value' => $_GET['destination'] | |
| 80 ); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 /** | |
| 85 * Parcoure le tableau contenant le formulaire à la recherche | |
| 86 * d'élement à modifier. | |
| 87 * @return | |
| 88 * TRUE si un élement a été modifié, FALSE sinon | |
| 89 */ | |
| 90 function _noderef_view_walk_form(&$form, $fields) { | |
| 91 $alter = FALSE; | |
| 92 foreach($form as $key => $item) { | |
| 93 if (is_array($item)) { | |
| 94 if (isset($fields[$key])) { | |
| 95 $alter |= noderef_view_alter_item($form, $key, $fields[$key]); | |
| 96 } | |
| 97 else { | |
| 98 $alter |= _noderef_view_walk_form($item, $fields); | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 return $alter; | |
| 103 } | |
| 104 | |
| 105 /** | |
| 106 * Modification d'un élement en particulier du formulaire. | |
| 107 * | |
| 108 * @return | |
| 109 * TRUE si l'element a été modifié, FALSE sinon. | |
| 110 */ | |
| 111 function noderef_view_alter_item(&$form, $key, $field) { | |
| 112 if (is_array($field) && $field['type'] == 'nodereference') { | |
| 113 list($link, $id) = _noderef_view_get_link($field); | |
| 114 if (!empty($link)) { | |
| 115 $form[$key]['#prefix'] .= "<div id='noderef_view_link_{$id}'>"; | |
| 116 $form[$key]['#suffix'] = $link .'</div>'. $form[$key]['#suffix']; | |
| 117 return TRUE; | |
| 118 } | |
| 119 } | |
| 120 return FALSE; | |
| 121 } | |
| 122 | |
| 123 /** | |
| 124 * Création d'un lien vers une éventuelle vue associé au champ. | |
| 125 * | |
| 126 * @return | |
| 127 * - Un lien vers la vue si une vue est associée | |
| 128 * - Une chaine vide sinon | |
| 129 */ | |
| 130 function _noderef_view_get_link($field) { | |
| 131 static $id = 0; | |
| 132 $path = ''; | |
| 133 // Check if there's a view associated with this field | |
| 134 if (isset($field['advanced_view']) && $field['advanced_view'] !== '--') { | |
| 135 $view = views_get_view($field['advanced_view']); | |
| 136 // Try to find a path for this view, looking at the displays | |
| 137 $path = ''; | |
| 138 foreach($view->display as $display) { | |
| 139 if (is_array($display->display_options) | |
| 140 && isset($display->display_options['path'])) { | |
| 141 $path = $display->display_options['path']; | |
| 142 break; | |
| 143 } | |
| 144 } | |
| 145 } | |
| 146 // If we found a view with a suitable display, then link to it | |
| 147 if (!empty($path)) { | |
| 148 $id++; | |
| 149 popups_add_popups(); | |
| 150 $options = array( | |
| 151 'attributes' => array( | |
| 152 'class' => 'popups noderef_view_link', | |
| 153 'rel' => 'noderef_view_link_'. $id), | |
| 154 'query' => array('destination' => 'node') | |
| 155 ); | |
| 156 $path = l(t('Search'), $path, $options); | |
| 157 } | |
| 158 return array($path, $id); | |
| 159 } | |
| 160 |
