我是Drupal的新手,但已经完成了创建小部件的任务。
模块加载,显示字段,但是当我创建一些内容时,在保存内容时不会存储表单值。
.install文件:
function cloudinary_field_schema($field) {
$columns = array(
'publicID' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
'url' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
'caption' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
'alt' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
'credit' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE)
);
return array(
'columns' => $columns,
'indexes' => array(),
);
}
.module文件:
/**
* Implements hook_field_info().
* define the field type
*/
function cloudinary_field_info() {
return array(
'cloudinary' => array(
'label' => t('Cloudinary image selector'),
'description' => t('Search for images and select one for display.'),
'default_widget' => 'cloudinary_widget',
'default_formatter' => 'cloudinary_formatter',
'settings' => array(),
'instance_settings' => array(),
)
);
}
/**
* This is the dropdown options for widget
*/
function cloudinary_field_widget_info() {
return array(
'cloudinary_widget' => array(
'label' => t('Default'),
'field types' => array('cloudinary', 'text'),
'behaviours' => array('multiple values' => 'FIELD_BEHAVIOUR_DEFAULT'),
)
);
}
/**
*/
function cloudinary_field_widget_form(&$form, &$form_state, $field, $instance, $lang, $items, $delta, $element) {
$element += array(
'#type' => 'fieldset',
);
$item =& $items[$delta];
$element['field_cloudinary_publicID'] = array(
'#title' => t('Public ID'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => isset($item['field_cloudinary_publicID']) ? $item['field_cloudinary_publicID'] : '',
);
$element['field_cloudinary_url'] = array(
'#title' => t('Url'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => isset($item['field_cloudinary_url']) ? $item['field_cloudinary_url'] : '',
);
$element['field_cloudinary_caption'] = array(
'#title' => t('Caption'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => isset($item['field_cloudinary_caption']) ? $item['field_cloudinary_caption'] : '',
);
$element['field_cloudinary_alt'] = array(
'#title' => t('Alt'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => isset($item['field_cloudinary_alt']) ? $item['field_cloudinary_alt'] : '',
);
$element['field_cloudinary_credit'] = array(
'#title' => t('Credit'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => isset($item['field_cloudinary_credit']) ? $item['field_cloudinary_credit'] : '',
);
return $element;
}
function cloudinary_field_is_empty($item, $field) {
$flag = FALSE;
foreach ($item as $key => $value) {
if(empty($key[$value])) {
$flag = TRUE;
}
}
return !$flag;
}
function cloudinary_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
if (!isset($item['field_cloudinary_publicID']) ||
!isset($item['field_cloudinary_url']) ||
!isset($item['field_cloudinary_caption']) ||
!isset($item['field_cloudinary_alt']) ||
!isset($item['field_cloudinary_credit'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'cloudinary_fields_missing',
'message' => t('%title: Make sure all fields are completed. '.
'Make sure all fiends are entered.',
array('%title' => $instance['label'])
),
);
}
}
}
function cloudinary_field_widget_error($element, $error, $form, &$form_state) {
switch ($error['error']) {
case 'cloudinary_fields_missing':
form_error($element, $error['message']);
break;
}
}
function cloudinary_field_formatter_info() {
return array(
'cloudinary_formatter' => array(
'label' => t('Default'),
'field types' => array('cloudinary'),
),
);
}
function cloudinary_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $item) {
$element[$delta] = cloudinary_format_field($item);
}
return $element;
}
function poutine_maker_format_field($item) {
$element = array(
'#type' => 'container',
'#attributes' => array( 'class' => array( 'field-item') ),
);
$element['field_cloudinary_publicID'] = array(
'label' => array(
'#type' => 'container',
'#attributes' => array( 'class' => array( 'field-label' )),
'text' => array(
'#markup' => t('Public ID'),
),
),
'item' => array(
'#type' => 'container',
'#attributes' => array( 'class' => array( 'field-item') ),
'text' => array(
'#markup' => $item['field_cloudinary_publicID'],
),
),
);
$element['field_cloudinary_url'] = array(
'label' => array(
'#type' => 'container',
'#attributes' => array( 'class' => array( 'field-label' )),
'text' => array(
'#markup' => t('Image Url'),
),
),
'item' => array(
'#type' => 'container',
'#attributes' => array( 'class' => array( 'field-item') ),
'text' => array(
'#markup' => $item['field_cloudinary_url'],
),
),
);
$element['field_cloudinary_caption'] = array(
'label' => array(
'#type' => 'container',
'#attributes' => array( 'class' => array( 'field-label' )),
'text' => array(
'#markup' => t('Image Caption'),
),
),
'item' => array(
'#type' => 'container',
'#attributes' => array( 'class' => array( 'field-item') ),
'text' => array(
'#markup' => $item['field_cloudinary_caption'],
),
),
);
$element['field_cloudinary_alt'] = array(
'label' => array(
'#type' => 'container',
'#attributes' => array( 'class' => array( 'field-label' )),
'text' => array(
'#markup' => t('Image Alt'),
),
),
'item' => array(
'#type' => 'container',
'#attributes' => array( 'class' => array( 'field-item') ),
'text' => array(
'#markup' => $item['field_cloudinary_alt'],
),
),
);
$element['field_cloudinary_credit'] = array(
'label' => array(
'#type' => 'container',
'#attributes' => array( 'class' => array( 'field-label' )),
'text' => array(
'#markup' => t('Image Credit'),
),
),
'item' => array(
'#type' => 'container',
'#attributes' => array( 'class' => array( 'field-item') ),
'text' => array(
'#markup' => $item['field_cloudinary_credit'],
),
),
);
return $element;
}
function cloudinary_format_canvas_field($item) {
drupal_add_js(drupal_get_path('module', 'cloudinary') . '/cloudinary.js');
}
答案 0 :(得分:1)
您需要在.install文件中实现hook_field_schema。
// Something like this
function cloudinary_field_schema($field) {
// $field['type']
return array(
'columns' => array(
'column_name' => array(
'type' => 'float',
'size' => 'big',
'not null' => TRUE,
'default' => 0,
),
)
);
}
答案 1 :(得分:1)
这是cloudinary_field_is_empty()中的逻辑问题。它总是回归虚假。一切正常!谢谢你的帮助Zolyboy!