سؤال

I'm new to Drupal but have been given the task of creating a widget.

The module loads, fields are displayed, but when I create some content the form values are not being stored when the content is being saved.

.install file:

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 file:

/**
 * 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');
}
هل كانت مفيدة؟

المحلول 2

It was a logic problem in cloudinary_field_is_empty(). It was always returning false. All working fine now! Thanks for your help Zolyboy!

نصائح أخرى

You need to implement hook_field_schema in your .install file.

// 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,
   ),
  )
 );
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top