Display Field conditional on Node Type

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
kevin.mcnamee@mailbox.org's picture

Hi,

I am using Draggable tables to organise nodes. One node type is defined as parent for the other node types. I wanted to display a "delete" link beside the child nodes, but not the parent nodes.

The solution I came up with was to create a Views handler based on the "Custom text" handler. The user configures which field in the View containes the node type of the current node (the field must be added to the View of course and probably excluded from the display). The user also selects which node types should control whether the custom text is displayed (or not). The custom text itself can be made up of tokens from other fields as usual.

Is there a better way or cleaner way?

Best regards,
Kevin

mymodule.view.inc:

<?php
/<strong>
*
Implementation of hook_views_handlers().
*/
function
mymodule_views_handlers() {
 
$path = drupal_get_path('module', 'mymodule') . '/views';

 
$data['handlers']['mymodule_handler_field_custom_conditional'] = array(
   
'parent' => 'views_handler_field_custom',
   
'path' => "$path",
  );

  return
$data;
}

/</
strong>
*
Implementation of hook_views_data().
*/
function
mymodule_views_data() {
 
$data = array();

 
$data['views']['type_conditional'] = array(
   
'title' => t('Custom text conditional (My Module)'),
   
'help' => t('Provide custom text or link if it matches specific node type(s).'),
   
'field' => array(
     
'handler' => 'mymodule_handler_field_custom_conditional',
     
'click sortable' => FALSE,
    ),
  );

  return
$data;
}
?>

mymodule_handler_field_custom_conditional.inc:

<?php
/<strong>
* @
file
* Contains the mymodule_handler_field_custom_conditional field handler.
*/

/</
strong>
*
Display conditional information based on node type.
*/
class
mymodule_handler_field_custom_conditional extends views_handler_field_custom {

  function
option_definition() {
   
$options = parent::option_definition();

   
$options['mymodule']['node_type_field'] = array('default' => '');
   
$options['mymodule']['operator'] = array('default' => 'in');
   
$options['mymodule']['value'] = array('default' => array());

    return
$options;
  }

  /<
strong>
   *
Define the extra form elements for the field.
   */
  function
options_form(&$form, &$form_state) {
   
parent::options_form($form, $form_state);

   
$form['mymodule'] = array(
     
'#type' => 'fieldset',
     
'#title' => t('Conditional display on'),
    );

   
$form['mymodule']['node_type_field'] = array(
     
'#type' => 'textfield',
     
'#title' => 'Node type field',
     
'#default_value' => $this->options['mymodule']['node_type_field'],
     
'#description' => t('The field containing the node type as per the "Replacement patterns" above, e.g. "[type]". The checkbox "Output machine name" must be checked for that field.'),
    );

   
module_load_include('inc', 'views', 'handlers/views_handler_filter');
   
module_load_include('inc', 'views', 'handlers/views_handler_filter_in_operator');
   
module_load_include('inc', 'views', 'modules/node/views_handler_filter_node_type');

   
// The node type filter handler has all the necessary code for selecting node types.
   
$filter_node_type = new views_handler_filter_node_type();

   
// Silence error.
   
$filter_node_type->options['expose']['available_operators'] = array();

   
// Borrowed from views_handler_filter.inc.
   
$form['op_val_start'] = array('#value' => '<div class="clear-block">');
   
$filter_node_type->show_operator_form($form, $form_state);
   
$filter_node_type->show_value_form($form, $form_state);
   
$form['op_val_end'] = array('#value' => '</div>');

   
// Reorganise form.
   
$form['mymodule']['operator'] = $form['operator'];
   
$form['mymodule']['value'] = $form['value'];
    unset(
$form['operator']);
    unset(
$form['value']);

   
// Set default values.
   
$form['mymodule']['operator']['#default_value'] = $this->options['mymodule']['operator'];
   
$form['mymodule']['value']['#default_value'] = $this->options['mymodule']['value'];
  }

  /</
strong>
   *
Render the field.
   */
  function
advanced_render($values) {
   
$value = parent::advanced_render($values);

   
$node_type_field = $this->options['mymodule']['node_type_field'];
   
$selected_node_types = array_values($this->options['mymodule']['value']);
   
$operator = $this->options['mymodule']['operator'];

   
$current_type = $this->last_tokens[$node_type_field];

   
// Default to base class behaviour if the field type is not found.
   
if (empty($current_type)) {
      return
$value;
    }

    if (
$operator == 'in') {
      if (
in_array($current_type, $selected_node_types, TRUE)) {
        return
$value;
      }
    } else {
      if (!
in_array($current_type, $selected_node_types, TRUE)) {
        return
$value;
      }
    }

  }

}
?>

Comments

Drupal 6

kevin.mcnamee@mailbox.org's picture

I should mention that I am working in Drupal 6.

Correction

kevin.mcnamee@mailbox.org's picture

Hi,

the last piece of render code should look like this:

<?php
   
if ($operator == 'in') {
      if (
in_array($current_type, $selected_node_types, TRUE)) {
        return
$value;
      }
    } elseif (
$operator == 'not in') {
      if (!
in_array($current_type, $selected_node_types, TRUE)) {
        return
$value;
      }
    } else {
      return
$value;
    }
?>

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week