You are here

workflowfield.field.inc in Workflow 7

Same filename and directory in other branches
  1. 7.2 workflow_field/workflowfield.field.inc

Defines a Workflow field, widget and formatter. (copied from list field).

File

workflow_field/workflowfield.field.inc
View source
<?php

/**
 * @file
 * Defines a Workflow field, widget and formatter. (copied from list field).
 */

/**
 * Implements hook_field_info().
 */
function workflowfield_field_info() {
  return WorkflowItem::getInfo();
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Changes the hook_field_settings_form.
 * Fixes some Field settings and Field Instance settings, and makes sure users cannot change it.
 * @todo: perhaps this is core functionality, but these values are only saved 
 *        when the site builder explicitly save the instance settings. :-(
 */
function workflowfield_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#field']['type'] == 'workflow') {

    // The Workflow field must have a value, so set to required.
    $form['instance']['required']['#type'] = 'hidden';
    $form['instance']['required']['#default_value'] = 1;
    $form['instance']['required']['#disabled'] = TRUE;

    // User may not set a default value, this is done by the Workflow module, @see WorkflowState->getOptions().
    $form['instance']['default_value_widget']['#type'] = 'hidden';
    $form['instance']['default_value_widget']['#disabled'] = TRUE;
    unset($form['instance']['default_value_widget']);

    // Make sure only 1 value can be entered in the Workflow field.
    $form['field']['cardinality']['#type'] = 'hidden';
    $form['field']['cardinality']['#default_value'] = 1;
    $form['field']['cardinality']['#disabled'] = TRUE;
  }
}

/**
 * Implements hook_field_settings_form().
 */
function workflowfield_field_settings_form($field, $instance, $has_data) {
  $form = array();
  $form_state = array();
  $workflow_field = new WorkflowItem($field, $instance);
  return $workflow_field
    ->settingsForm($form, $form_state, $has_data);
}

/**
 * We will be using some default formatters and widgets from the List and Options modules.
 */

/**
 * Implements hook_field_formatter_info_alter().
 *
 * The module reuses the formatters defined in list.module.
 */
function workflowfield_field_formatter_info_alter(&$info) {
  $info['list_default']['field types'][] = 'workflow';
}

/**
 * Implements hook_field_widget_info_alter().
 *
 * The module does not implement widgets of its own, but reuses the
 * widgets defined in options.module.
 *
 * @see workflowfield_options_list()
 */
function workflowfield_field_widget_info_alter(&$info) {
  $info['options_select']['field types'][] = 'workflow';
  $info['options_buttons']['field types'][] = 'workflow';
}

/**
 * Do not implement hook_field_presave(), 
 * since $nid is needed, but not yet known at this moment.
 */

//function workflowfield_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {

//}

/**
 * Implements hook_field_insert().
 */
function workflowfield_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  return workflowfield_field_update($entity_type, $entity, $field, $instance, $langcode, $items);
}

/**
 * Implements hook_field_update().
 * It is called also from hook_field_insert, since we need $nid to store workflow_node_history.
 * We cannot use hook_field_presave, since $nid is not yet known at that moment.
 */
function workflowfield_field_update($entity_type, $entity, array $field, $instance, $langcode, &$items) {
  $form = array();
  $form_state = array();
  if ($entity_type == 'comment') {

    // This happens when we are on an entity's comment.
    $nid = isset($entity->nid) ? $entity->nid : 0;
    $referenced_entity_type = 'node';

    // Comments only exist on nodes.
    $referenced_entities = entity_load($referenced_entity_type, array(
      $nid,
    ));
    $referenced_entity = $referenced_entities[$nid];

    // @todo: when changing Workflow via comment, the history is saved twice.
    $widget = new WorkflowDefaultWidget($field, $instance, $referenced_entity_type, $referenced_entity);
    $widget
      ->submit($form, $form_state, $items);

    // $items is a proprietary D7 parameter.
    // We must save the node, too.
    $widget
      ->entitySave($referenced_entity_type, $referenced_entity);
  }
  else {
    $widget = new WorkflowDefaultWidget($field, $instance, $entity_type, $entity);
    $widget
      ->submit($form, $form_state, $items);

    // $items is a proprietary D7 parameter.
    //    $widget->setEntity($entity_type, $entity);
    //    $widget->entitySave($entity_type, $entity);
  }
}

/**
 * Implements hook_field_delete().
 *
 * @todo: implement
 */

//function workflowfield_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {

//  $workflow_field = new WorkflowItem($field, $instance, $entity_type, $entity);
//  $workflow_field->delete($items);

//}

/**
 * Implements hook_field_is_empty().
 *
 * The Workflow field is never empty.
 */
function workflowfield_field_is_empty($item, $field) {

  //  $workflow_field = new WorkflowItem($field, $instance, $entity_type, $entity);
  //  $workflow_field->isEmpty($item);
  return FALSE;
}

/**
 * Implements hook_field_delete_field().
 *
 * @todo: implement functionality from workflow_node_delete().
 */

//function workflowfield_field_delete_field($entity_type, $entity, $field, $instance, $langcode, &$items) {

//}

/*
 * Callback function for list.module formatter.
 *
 * Returns the array of allowed values for a list field.
 * @see list_allowed_values()
 */
function workflowfield_allowed_values($field, $instance, $entity_type, $entity) {
  $workflow_field = new WorkflowItem($field, $instance, $entity_type, $entity);
  return $workflow_field
    ->getAllowedValues();
}

/**
 * Callback function for the default Options widgets.
 * Implements hook_options_list().
 * @todo: move to a class.
 */
function workflowfield_options_list($field, $instance, $entity_type, $entity) {
  $workflow_field = new WorkflowItem($field, $instance, $entity_type, $entity);
  return $workflow_field
    ->getOptions();
}

/*
 * Start a simpletest.
 */
function __workflowfield_test() {
  module_enable(array(
    'simpletest',
  ), $enable_dependencies = TRUE);
  $wf_webtest = new WorkflowWebTest();
  $wf_webtest
    ->testWorkflow();
  $wf_webtest
    ->testWorkflowState();
}

Functions

Namesort descending Description
workflowfield_allowed_values
workflowfield_field_formatter_info_alter Implements hook_field_formatter_info_alter().
workflowfield_field_info Implements hook_field_info().
workflowfield_field_insert Implements hook_field_insert().
workflowfield_field_is_empty Implements hook_field_is_empty().
workflowfield_field_settings_form Implements hook_field_settings_form().
workflowfield_field_update Implements hook_field_update(). It is called also from hook_field_insert, since we need $nid to store workflow_node_history. We cannot use hook_field_presave, since $nid is not yet known at that moment.
workflowfield_field_widget_info_alter Implements hook_field_widget_info_alter().
workflowfield_form_field_ui_field_edit_form_alter Implements hook_form_FORM_ID_alter().
workflowfield_options_list Callback function for the default Options widgets. Implements hook_options_list(). @todo: move to a class.
__workflowfield_test