'Nodereference graph', 'page callback' => 'graphviz_noderef_page', 'page arguments' => array(1), 'access arguments' => array('access Graphviz Noderef display'), 'type' => MENU_LOCAL_TASK, ); return $items; } /** * options form for the node tab. */ function graphviz_noderef_page_form($form_state) { $options = drupal_map_assoc(range(0, 5)); // $form['title'] = array( // '#type' => 'textfield', // '#title' => t('Graph title'), // '#default_value' => isset($_POST['title']) ? $_POST['title'] : "" , // '#maxlength' => 64, // '#description' => t('title of the graph'), // ); $form['recursion_depth'] = array( '#type' => 'select', '#title' => t('Recursion depth'), '#default_value' => isset($_POST['recursion_depth']) ? $_POST['recursion_depth'] : 1 , '#options' => $options, '#description' => t('depth to continue searching. 0 only returns nodes 1 step away, 5 returns nodes up to 6 steps away'), ); $options = array('dot' => 'dot','neato' => 'neato','fdp' => 'fdp','twopi' => 'twopi','circo' => 'circo'); $form['command'] = array( '#type' => 'select', '#title' => t('Command'), '#default_value' => isset($_POST['command']) ? $_POST['command'] : "dot" , '#options' => $options, '#description' => t('which graphviz command to use to render the graph.'), ); $options = array('png'=> t("png"),'jpg' => t("jpg"),'gif' => t("gif"),'svg'=> t("svg")); $form['formats'] = array( '#type' => 'checkboxes', '#title' => t('Output Formats'), '#default_value' => isset($_POST['formats']) ? $_POST['formats'] : array('png') , '#options' => $options, '#description' => t('Select which graphics output formats to render to.') ); $form['imagemap'] = array( '#type' => 'checkbox', '#title' => t('Imagemap'), '#default_value' => isset($_POST['imagemap']) ? $_POST['imagemap'] : true , ); $form['graphoptions'] = array( '#type' => 'textfield', '#title' => t('Graph options'), '#default_value' => isset($_POST['graphoptions']) ? $_POST['graphoptions'] : "label=\"\", labelloc=\"t\", overlap=false, splines=true, sep=0.2" , '#description' => t('Display options for the entire graph.'), ); $form['nodeoptions'] = array( '#type' => 'textfield', '#title' => t('Graph title'), '#default_value' => isset($_POST['nodeoptions']) ? $_POST['nodeoptions'] : "fontsize=8.0, style=filled, fillcolor=\"#ffeedd\"" , '#description' => t('Display options for nodes.'), ); $form['edgeoptions'] = array( '#type' => 'textfield', '#title' => t('Graph title'), '#default_value' => isset($_POST['edgeoptions']) ? $_POST['edgeoptions'] : "color=\"lightblue\", penwidth=3, fontsize=8.0" , '#description' => t('Display options for edges.'), ); $form['submit'] = array('#type' => 'submit', '#value' => t('Save')); $form['#redirect'] = FALSE; return $form; } /** * Page callback for node/%node/graph (See graphviz_noderef_menu()). */ function graphviz_noderef_page($node) { $output = drupal_get_form('graphviz_noderef_page_form'); $recursion_depth = isset($_POST['recursion_depth']) ? $_POST['recursion_depth'] : 1; $command = isset($_POST['command']) ? $_POST['command'] : "dot"; $formats = isset($_POST['formats']) ? $_POST['formats'] : array('png'); $imagemap = isset($_POST['imagemap']) ? "TRUE" : "FALSE" ; // $title= isset($_POST['title']) ? $_POST['title'] : ""; $graph =isset($_POST['graphoptions']) ? $_POST['graphoptions'] : "label=\"\", labelloc=\"t\", overlap=false, splines=true, sep=0.2"; $nodes =isset($_POST['nodeoptions']) ? $_POST['nodeoptions'] : "fontsize=8.0, style=filled, fillcolor=\"#ffeedd\""; $edges =isset($_POST['edgeoptions']) ? $_POST['edgeoptions'] : "color=\"lightblue\", penwidth=3, fontsize=8.0"; $headers = array('command'=>$command,'formats'=>$formats,'imagemap'=>$imagemap, 'graph'=>$graph,'nodes'=>$nodes,'edges'=>$edges); $output .= graphviz_noderef_render($node, $recursion_depth, $headers); return $output; } /** * Render the map!. */ function graphviz_noderef_render($node, $recursion_depth = 1, array $headers) { $tree = graphviz_noderef_get_ref_array((int)$node->nid,(int)$recursion_depth); $tree[2][(int)$node->nid]['target'] = true; $dot = graphviz_noderef_data_to_dot($tree[0], $tree[2], $headers); $output = graphviz_filter_process($dot); // $output = "
$dot" . $output ; return $output; } /** * Custom functions for gathering and formatting data. * **/ function graphviz_noderef_data_to_dot(array $ref_array, array $parameters = array(), array $headers = array(), array $style = array()) { //initialize output $output = ""; //print headers $output .= graphviz_noderef_headers_to_dot($headers); //convert the parameters to dot format. $output .= graphviz_noderef_params_to_dot($parameters); //convert links array to dot format links $output .= graphviz_noderef_refs_to_dot($ref_array, $style); //print end $output .="};"; return $output; } /** * ouptut the actual node-network data in dot format. **/ function graphviz_noderef_refs_to_dot(array $ref_array, array $style = array()){ //convert links array to dot format links $link_array = array(); foreach ($ref_array as $link){ if ($link['type'] == "nodereference") { $from = $link['nid']; $to = $link['ref']; $field_name = $link['field']; } elseif ($link['type'] == "nodereferrer"){ $from = $link['ref']; $to = $link['nid']; $field_name = $link['referrer']; } $action = str_replace(array("field_","_"),array(""," "), $field_name); $link_array[] .= "\t$from -> $to [ label = \"$action\" ];\n"; } $output = join(array_unique($link_array)); return $output; } /** * print headers that tell graphviz_filter_process how to render the graph. **/ function graphviz_noderef_headers_to_dot(array $options = array() ) { $defaults = array('command' => "dot", 'formats' => "png", 'imagemap' => true); $headers = array_merge($defaults,$options); foreach ($headers as $key => $value) { switch ($key) { case 'command': $command = $value; break; case 'formats': $formats = join($value); break; case 'imagemap': $imagemap = ($value == true) ? "true" : "false" ; break; case 'graph': $graph = $value; break; case 'nodes': $nodes = $value; break; case 'edges': $edges = $value; break; } } $output = <<