Field API

Custom fields

Цели

- Создание модуля

- Создание поля

- Организация вывода

- Альтернативы

Field API

Создадим поле, которое будет хранить связку с термином словаря, датой, и двумя зависимыми текстовыми полями.

examplefieldapid7

examplefieldapid7.info

examplefieldapid7.install

examplefieldapid7.module

examplefieldapid7.info

name = Custom field type 
examplecore = 7.x
package = Examples
version = "7.x-1.0"
project = "examplefieldapid7"

dependencies[] = field


examplefieldapid7.install

/**
 * Hook hook_field_schema
 */
function examplefieldapid7_field_schema($field) {
  switch ($field['type']) {
    case 'field_customtype' :
      $columns = array(
        'tid' => array(
          'type' => 'int',
          'default' => 0,
        ),
        ...
      );
      $indexes = array();
    break;
  }
 
  return array(
    'columns' => $columns,
    'indexes' => $indexes
  );
}

examplefieldapid7.module

// Объявляем поле (тип, виджет и форматтер)
examplefieldapid7_field_info() { ... }

// объявляем виджет
examplefieldapid7_field_widget_info() { ... } 

// объявляем форматтер
examplefieldapid7_field_formatter_info() { ... } 

// функция для определения пустой конструкции
examplefieldapid7_field_is_empty() { ... } 

// функция для определения валидации
_examplefieldapid7_field_validate() { ... }

// темизация 
examplefieldapid7_theme() { ... }

examplefieldapid7_field_info()

/**
 * Implements hook_field_info().
 */
function examplefieldapid7_field_info() {
  return array(
    'field_customtype' => array(
      'label' => t('Custom field'),
      'description' => t('Custom field'),
      'default_widget' => 'field_customtype_default_widget',
      'default_formatter' => 'field_customtype_default_formatter',
    ),
  );
}

examplefieldapid7_field_widget_info()

/**
 * Implements hook_field_widget_info().
 */
function examplefieldapid7_field_widget_info() {
  return array(
    'field_customtype_default_widget' => array(
      'label' => t('Custom field widget'),
      'field types' => array('field_customtype'),
    ),
  );
}

examplefieldapid7_field_formatter_info()

/**
 * Implements hook_field_formatter_info().
 */
function examplefieldapid7_field_formatter_info() {
  return array(
    'field_customtype_default_formatter' => array(
      'label' => t('Default formatter for custom type field'),	
      'field types' => array('field_customtype'),
    ),
  );
}

examplefieldapid7_field_widget_form()

/**
 * Implements hook_field_widget_form().
 */
function examplefieldapid7_field_widget_form(&$form, &$form_state, $field, $instance, 
  $langcode, $items, $delta, $element) {
  $base = $element;
 
  switch ($instance['widget']['type']) {
    case 'field_customtype_default_widget':
      $element['#element_validate'] = array('_examplefieldapid7_field_validate');
      $element['customtype_widget_wrapper']['#theme'] = 'customtype_widget_wrapper';

      $element['customtype_widget_wrapper']['text'] = array(
        '#type' => 'textfield',
        '#title' => 'Текст',
        '#default_value' => isset($items[$delta]['text']) ? $items[$delta]['text']: NULL,
      ) + $base;
      ...
    break;
  }
  return $element;
}

theme_customtype_widget_wrapper()

Пример темизации виджета

/**
 * Returns HTML for a default field widget.
 */
function theme_customtype_widget_wrapper($variables) {
  $element = $variables['element'];
 
  $output = '';
 
  $output .= '<div class="custom-type-field-item">';
  $output .= drupal_render($variables['element']['text']);
  $output .= '</div>';
  ...
 
  return $output;
}

Результаты

Результаты

Результаты

Результаты

Don't use Kostili&Velosipedi methods

Field API

dcafe

By t1mm1

dcafe

  • 1,045