I am trying to create a custom field type widget with a ajax submit handler to save the value of the field to the database without saving the whole node.
Here is my code hook_field_field_info().
function mymodule_field_info() {
return array(
'time_logger_time' => array(
'label' => t('Time loger'),
'description' => t('Custom Poutine Field'),
'default_widget' => 'time_logger_time_widget',
'default_formatter' => 'time_logger_default_formatter',
),
);
}
Than this is my hook_field_widget_info()
function mymodule_field_widget_info() {
return array(
'time_logger_time_widget' => array(
'label' => t('Default'),
'field types' => array('time_logger_time'),
),
);
}
And finally my hook_field_widget_form() looks like
function mymodule_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$value = isset($items[$delta]['id']) ? $items[$delta]['id'] : '';
$element['id'] = array(
'#type' => 'textfield',
'#size' => 6,
'#default_value' => $value
);
$element[submit] = array(
'#type' => 'button',
'#value'=>'Save',
'#name' => 'submit-'. $delta,
'#ajax' => array(
'callback' => 'mymodule_ajax_handler',
'effect' => 'fade',
),
);
return $element;
}
Idea is when I click the save button in my widget form it will call my ajax callback load the node, set the value of my field and save the node as follows-
function mymodule_ajax_handler( $form, &$form_state){
$node = node_load(22);
dsm( $node);
$node_wrapper = entity_metadata_wrapper('node', $node);
$node_wrapper->field_log->set(1);
$node->save();
}
But when I click button I am getting some error saying "Notice: Undefined index: field_log in EntityStructureWrapper->getPropertyInfo() (line 334 of /var/www/html/mysite/sites/all/modules/contrib/entity/includes/entity.wrapper.inc)."
If I change my field in the ajax callback to some other field in my content type its saving fine. Like below-
function mymodule_ajax_handler( $form, &$form_state){
$node = node_load(22);
$node_wrapper = entity_metadata_wrapper('node', $node);
$node_wrapper->some_other_field_from_my_content_type->set(1); //This works fine
$node->save();
}
Is there anything wrong in my widget form.
Here is my field scheme if someone want to refer-
function mymodule_field_schema($field) {
$schema = array();
$schema['columns']['id'] = array(
'type' => 'varchar',
'length' => 50,
'not null' => FALSE
);
return $schema;
}
field_log
? That name is the system name you give on the Manage Fields page. It can differ per user, so you should not be making assumptions on that name unless you manually create and lock the fields and field instances in eghook_enable()
. – Neograph734 May 22 '16 at 23:24field_log2
it will never work because the namefield_log
is hardcoded in your AJAX callback. You should not hardcode field names in your modules if a user can freely choose those names. – Neograph734 May 23 '16 at 13:55