Ok so I have created a drupal module that retrieves data stored in a database. The field in question contains html code for its value. I want to take that html code and have it utilized for what its value is in drupal. So if it has a list in the code I want it to display the list so on and so forth.

Here is my code for my module.

<?php

/**
* @file
*/

/**
 * Implements hook_views_data()
 */
function jobs_views_data() {
  $data['Jobs']['table']['group'] = t('Jobs');
  $data['Jobs']['table']['base'] = array(
    'field' => 'JobID',
    'title' => t('Jobs'),
    'help' => t('This table shows a list of all jobs.'),
    'database' => 'MyDatabase'
  );


  $data['Jobs']['JobID'] = array(
      'title' => t('Job ID'),
      'help' => t('A unique job id'),
      'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
      'argument' => array(
      'handler' => 'views_handler_argument_numeric',
    ),
      'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
      'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  $data['Jobs']['Active'] = array(
      'title' => t('Active'),
      'help' => t('A field representing whether or not a job is active.'),
      'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
      'argument' => array(
      'handler' => 'views_handler_argument_numeric',
    ),
      'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
      'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  $data['Jobs']['DivisionID'] = array(
      'title' => t('Division ID'),
      'help' => t('A unique division id associated with a job.'),
      'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
      'argument' => array(
      'handler' => 'views_handler_argument_numeric',
    ),
      'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
      'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  $data['Jobs']['JobTitle'] = array(
      'title' => t('Job Title'),
      'help' => t('Job title'),
      'field' => array(
      'click sortable' => TRUE,
    ),
      'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );


  $data['Jobs']['JobHTML'] = array(
      'title' => t('Job HTML'),
      'help' => t('Job HTML.'),
      'field' => array(
        'click sortable' => TRUE,
    ),
      'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );


  return $data;
}

So as you can see that last field there JobHTML needs a handler for the field. I have tried setting it to 'handler' => 'views_handler_field_markup'. Oddly enough it did seem to lay the html out correctly as it should, but then it left the HTML code inside of the content.

Comments

mobilemeanbunny’s picture

FINALLY I SOLVED IT! After literally months of trying to make this happen I have got it figured out.

So basically I had to write my own handler. Here is the code.

<?php

class jobs_handler_full_html extends views_handler_field {
  /**
   * Constructor; calls to base object constructor.
   */
  function construct() {
    parent::construct();

    $this->format = 'full_html';//$this->definition['format'];
  }

  function render($values) {
    $value = $this->get_value($values);
    if (is_array($this->format)) {
      $format = $this->get_value($values, 'format');
    }
    else {
      $format = $this->format;
    }
    if ($value) {
      $value = str_replace('<!--break-->', '', $value);
      return check_markup($value, $format, '');
    }
  }

  function element_type($none_supported = FALSE, $default_empty = FALSE, $inline = FALSE) {
    if ($inline) {
      return 'span';
    }

    if (isset($this->definition['element type'])) {
      return $this->definition['element type'];
    }

    return 'div';
  }
}

?>

Basically here I looked at the view handler for view_handler_field_markup and just removed the $this->definition section and put full_html. This way I forced it to use full_html for the format I wanted.

Then in my module code for hook_views_data() I did this.

  $data['Jobs']['JobHTML'] = array(
      'title' => t('Job HTML'),
      'help' => t('Job HTML that displays the job posting.'),
      'field' => array(
		'handler' => 'jobs_handler_full_html',
    ),
  );

In my module.info file I added the file.

files[] = jobs_handler_full_html.inc

That was pretty much it, I can't believe how simple this was yet no one could help me answer this question. I have posted to the drupal forums, stackoverflow, etc. I love drupal but when it comes to development there is little support.

jlstrecker’s picture

It probably didn't exist back when the question was asked, but now you can use the Views field handler plugin called Markup.

Example:

// In hook_views_data()
$data['my_table']['my_field__value'] = array(
'title' => t('My field'),
'field' => array(
'id' => 'markup',
'format' => 'full_html',
),
);