Create Custom Views Field

Declare dependency and views file handler to .info of your custom module.

Note: Italized string is your handler identifier. This example creates a custom field under entityform field collection. Create a relationship settings on your views to access the entityform field collection.

mycustom_module.info
dependencies[] = views
dependencies[] = entityform

files[] = views/mycustom_module_handler_myhandler.inc

Implement hook_views_api() in your custom module -- reference

 mycustom_module.module
/**
* Implements hook_views_api().
*/
function mycustom_module_views_api() {
  return array(
    'api' => 3, //version of the Views API
    'path' => drupal_get_path('module', 'mycustom_module') . '/views',
  );
}

Under your custom module directory, create a new folder "views".

Create a new file "mycustom_module.views.inc" inside "views" folder

mycustom_module/views/mycustom_module.views.inc
/**
* Implements hook_views_data_alter(). --reference
* Note: The first index on $data is 'entityform' because we want to add this field under entityform
* field collection
*/
function mycustom_module_views_data_alter(&$data) {
  $info = entity_get_info('entityform');
  $label = $info['label'];
  $data['entityform']['confirm_entityform'] = array(
      'field' => array(   
        'title' => t('Title of this handler'),
        'help' => t('Description of this handler'),
        'handler' => 'mycustom_module_handler_myhandler',
    ),
  );
}

Create the handler file.

mycustom_module/views/mycustom_module_handler_myhandler.inc
class mycustom_module_handler_myhandler extends entityform_handler_field
{

  //add required fields needed for render
  function construct()
  {
      parent::construct();
      $this->additional_fields['uid'] = 'uid';
  }

  function render($values)
  {
      $return_this = "";
      if ($entity = $this->get_value($values)) {
     
         //check values of custom field from an entityform collection or something
         if($entity->field_myfield) {
           $return_this = $entity->field_myfield["value"];          
         }                    
      }
      return $return_this;  
  }
} //end of class

No comments:

Post a Comment