Cómo Crear, actualizar y eliminar una entity Drupal 7
Drupal y Entities:
1. Crear Entity type desde Drupal
2. Crear Bundel
3. Crear el módulo que va contener lo siguiente:
<?php
function ejemplo_menu() {
$items['ejemplo/create-groups-popup/%ctools_js'] = array(
'title' => t('Create Group'),
'page callback' => 'ejemplo_create_groups_popup',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
// Receive a param: id_grupo
$items['ejemplo/edit-group-popup/%/%ctools_js'] = array(
'title' => t('Edit Group'),
'page callback' => 'ejemplo_edit_groups_popup',
'page arguments' => array(2,3),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['ejemplo/delete-group-popup/%'] = array(
'title' => t('Delete group'),
'page callback' => 'drupal_get_form',
'page arguments' => array('ejemplo_delete_groups_form', 2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['ejemplo/delete-group-popup/%'] = array(
'title' => t('Delete group'),
'page callback' => 'drupal_get_form',
'page arguments' => array('ejemplo_delete_groups_form', 2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
// desde Modal
function ejemplo_create_groups_popup($ajax){
if ($ajax) {
ctools_include('ajax');
ctools_include('modal');
$form_state = array(
'ajax' => TRUE,
'title' => t('Create Group'),
);
$output = ctools_modal_form_wrapper('ejemplo_create_group_form', $form_state); // LLamado al formulario dentro de la ventana modal
// if (!empty($form_state['ajax_commands'])) {
// $output = $form_state['ajax_commands'];
// }
if (!empty($form_state['executed'])) {
// Add the responder javascript, required by ctools
ctools_add_js('ajax-responder');
// Create ajax command array, dismiss the modal window.
$output = array();
$output[] = ctools_modal_command_dismiss();
$output[] = ctools_ajax_command_reload();
}
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('ejemplo_create_group_form');
}
}
// Validacion por defecto
function ejemplo_create_group_form_validate($form, &$form_state){
}
// hook submit para crear y actualizar
function ejemplo_create_group_form_submit($form, &$form_state){
switch ($form_state['input']['op']){
case t('Save'):
$enterprise_entity = entity_create('group', array('type' =>'general'));//group: Nombre del entity type y general: nombre del bundle
$enterprise_entity->title = $form_state['values']['nombre_campo']; // nombre titulo creado
$enterprise_entity->field_group_administrator = array(LANGUAGE_NONE => array(0 => array('target_id' => $form_state['values']['nombre_campo']))); //nombre del campo adicionado custom
$enterprise_entity->save();
drupal_set_message(t('Group created succesfully'), 'status', FALSE);
break;
case t('Update'):
// Load the entity
$idgroup = $form_state['values']['idgroup']; // de alguna manera obtener el campo primario
$group = entity_load('group', array($form_state['values']['idgroup']));
$group[$idgroup]->title = $form_state['values']['name_group'];
$group[$idgroup]->field_group_administrator['und'][0]['target_id'] = $form_state['values']['nombre_campo']; // Asignando valor nuevo
$group[$idgroup]->save();
drupal_set_message(t('Group has been updated succesfully'), 'status', FALSE);
break;
}
}
// eliminar una entidad con confirmacion
function ejemplo_delete_groups_form($form, &$form_state, $id_group){
$group = entity_load_single('group', $id_group);
$name_group = $group->title;
$form['#record'] = $id_group;
$form['#group'] = $name_group;
// Always provide entity id in the same form key as in the entity edit form.
//$form['ifid'] = array('#type' => 'value', '#value' => $source->ifid);
return confirm_form($form,
t("Are you sure you want to delete the group '$name_group'?", array('%title' => $name_group)),
'ejemplo/list-groups',
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* Execute source deletion
*/
function ejemplo_delete_groups_form_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$id_group = $form_state['complete form']['#record'];
entity_get_controller('group')->delete(array($id_group));
$form_state['redirect'] = 'ejemplo/list-groups';
//watchdog('content', '@type: deleted %title.', array('@type' => $node->type, '%title' => $node->title));
drupal_set_message(t(' %title has been deleted.', array('%title' => $form_state['complete form']['#group'])));
}
}

Comentarios
Publicar un comentario