【问题标题】:Drupal widget not saving field values [closed]Drupal小部件不保存字段值[关闭]
【发布时间】:2014-05-13 14:52:07
【问题描述】:

我是 Drupal 的新手,但我被分配了创建小部件的任务。

模块加载,显示字段,但是当我创建一些内容时,在保存内容时没有存储表单值。

.安装文件:

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');
}

【问题讨论】:

标签: php drupal-7


【解决方案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,
   ),
  )
 );
}

【讨论】:

  • 谢谢,我已经有了。我没有在 stackoverflow 编辑器中正确显示代码
  • 你检查过phpmyadmin中的表格吗?你在 .install 中定义的字段是否存在?
  • 好电话。我刚查了一下,它们是: field_cloudinary_publicID field_cloudinary_url field_cloudinary_caption field_cloudinary_alt field_cloudinary_credit
  • 另外,在出现的表单中,文本字段有这样的: field_cloudinary[und][0][field_cloudinary_publicID] ,看起来好像有一个未定义的变量以某种方式进入?
  • 你定义了 cloudinary_formatter 了吗?
【解决方案2】:

这是 cloudinary_field_is_empty() 中的逻辑问题。它总是返回错误。现在一切正常!感谢您的帮助 Zolyboy!

【讨论】:

    猜你喜欢
    • 2012-09-08
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    相关资源
    最近更新 更多