You are here

cmis_field.module in CMIS API 7

File

cmis_field/cmis_field.module
View source
<?php

/**
 * Implementation of hook_field_info()
 *
 * @return array - field definition
 */
function cmis_field_field_info() {
  return array(
    'cmis_field_path' => array(
      'label' => t('CMIS Field'),
      'description' => t('Attach a CMIS object to a Drupal entity'),
      'default_widget' => 'cmis_field_widget',
      'default_formatter' => 'cmis_field_link',
      'instance settings' => array(
        'cmis_field_rootFolderPath' => '',
      ),
    ),
  );
}

/**
 * Implements hook_field_validate().
 *
 * For now, just validates that the field isn't empty.
 */
function cmis_field_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {
    if (!empty($item['cmis_field_path'])) {
      if (1 == 0) {
        $errors[$field['cmis_field']][$langcode][$delta][] = array(
          'error' => 'cmis_field_invalid',
          'message' => t('You are in the anti-universe, where 1 == 0.'),
        );
      }
    }
  }
}

/**
 * Implementation of hook_is_empty()
 *
 * @param $item - field value(s)
 * @param $field - field settings
 * @return boolean - TRUE/FALSE
 */
function cmis_field_field_is_empty($item, $field) {
  return empty($item['path']);
}

/**
 * Implementation of hook_instance_settings_form()
 *
 * @param $op - operation
 * @param $field - field beign operated on
 * @return - form or settings array dependent on operation
 */
function cmis_field_field_instance_settings_form($field, $instance) {
  $settings = $instance['settings'];
  $form['cmis_field_rootFolderPath'] = array(
    '#title' => t('Root Directory'),
    '#description' => t('Root folder for CMIS nodes'),
    '#type' => 'textfield',
    '#default_value' => isset($settings['cmis_field_rootFolderPath']) ? $settings['cmis_field_rootFolderPath'] : '',
  );
  $form['#attached']['js'] = array(
    array(
      'data' => drupal_get_path('module', 'cmis_field') . '/js/cmis_field.js',
    ),
  );
  return $form;
}

/**
 * Implementation of hook_field_widget_info
 *
 * @return array defining the widget
 */
function cmis_field_field_widget_info() {
  return array(
    'cmis_field_browser' => array(
      'label' => t('CMIS link'),
      'field types' => array(
        'cmis_field_path',
      ),
      'settings' => array(
        'cmis_field_rootFolderPath' => array(),
      ),
    ),
  );
}

/**
 * Implementation of hook_field_widget_form()
 *
 * @param $element - the form element array
 * @param $edit -
 * @param $form_state - form state array
 * @param $form - form array
 * @return array - form element
 */
function cmis_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $title = isset($items[$delta]['title']) ? $items[$delta]['title'] : '';
  $path = isset($items[$delta]['path']) ? $items[$delta]['path'] : '';
  $element += array(
    '#delta' => $delta,
  );
  $element['path'] = array();
  $element['title'] = array();
  $element['title'] += array(
    '#type' => 'textfield',
    '#default_value' => $title,
    '#attributes' => array(
      'class' => array(
        'edit-field-cmis-field',
      ),
    ),
    '#title_display' => 'before',
    '#title' => check_plain($instance['label']),
  );
  $element['path'] += array(
    '#type' => 'hidden',
    '#attributes' => array(
      'class' => array(
        'edit-field-cmis-path',
        'search-form',
      ),
    ),
    '#default_value' => $path,
  );
  $element['#attached']['js'] = array(
    array(
      'data' => drupal_get_path('module', 'cmis_field') . '/js/cmis_field.js',
    ),
    array(
      'data' => array(
        'cmispath' => $instance['settings']['cmis_field_rootFolderPath'],
      ),
      'type' => 'setting',
    ),
  );
  return $element;
}

/**
 * Implementation of hook_field_formatter_info()
 *
 * @return array
 */
function cmis_field_field_formatter_info() {
  return array(
    'cmis_field_link' => array(
      'label' => t('CMIS browser'),
      'field types' => array(
        'cmis_field_path',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 *
 */
function cmis_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {
    case 'cmis_field_link':
      foreach ($items as $delta => $item) {
        $element[$delta]['#markup'] = l($item['title'], 'cmis/browser', array(
          'query' => array(
            'id' => $item['path'],
          ),
        ));
      }
      break;
  }
  return $element;
}

Functions

Namesort descending Description
cmis_field_field_formatter_info Implementation of hook_field_formatter_info()
cmis_field_field_formatter_view Implements hook_field_formatter_view().
cmis_field_field_info Implementation of hook_field_info()
cmis_field_field_instance_settings_form Implementation of hook_instance_settings_form()
cmis_field_field_is_empty Implementation of hook_is_empty()
cmis_field_field_validate Implements hook_field_validate().
cmis_field_field_widget_form Implementation of hook_field_widget_form()
cmis_field_field_widget_info Implementation of hook_field_widget_info