annotate noderef_view.module @ 7:281e131cb6c3

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