diff --git entity_example/entity_example.info entity_example/entity_example.info new file mode 100644 index 0000000..97f2c1e --- /dev/null +++ entity_example/entity_example.info @@ -0,0 +1,6 @@ +; $Id: ajax_example.info,v 1.3 2010/08/11 23:16:27 rfay Exp $ + +name = Entity Example +description = Demonstrates the use of the Entity API. +core = 7.x +package = Example modules diff --git entity_example/entity_example.install entity_example/entity_example.install new file mode 100644 index 0000000..930716a --- /dev/null +++ entity_example/entity_example.install @@ -0,0 +1,68 @@ + 'The base table for example entity', + 'fields' => array( + 'eeid' => array( + 'description' => 'The primary identifier for a entity.', + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'title' => array( + 'description' => 'The title of this entity, always treated as non-markup plain text.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + ), + 'perm' => array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'size' => 'tiny', + 'description' => 'Whether the example entity is permanent(1) or not(0).', + ), + 'dstart' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + 'description' => 'Start date of example entity. Format: Ymd', + ), + 'dend' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + 'description' => 'End date of example entity. Format: Ymd', + ), + 'weight' => array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'description' => 'The weight of this example entity.', + ), + ), + 'primary key' => array('eeid'), + 'indexes' => array( + 'perm' => array('perm'), + 'dstart' => array('dstart'), + 'dend' => array('dend'), + ), + ); + return $schema; +} diff --git entity_example/entity_example.module entity_example/entity_example.module new file mode 100644 index 0000000..e5713d5 --- /dev/null +++ entity_example/entity_example.module @@ -0,0 +1,351 @@ + array( + 'title' => t('Administer example entity'), + 'restrict access' => TRUE, + ), + ); +} + +/** + * Implements hook_menu(). + */ +function entity_example_menu() { + $items['examples/entity_example/%entity_example'] = array( + 'title' => 'Example entity', + 'title callback' => 'entity_example_page_title', + 'title arguments' => array(1), + 'page callback' => 'entity_example_page', + 'page arguments' => array(1), + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + $items['examples/entity_example/%entity_example/view'] = array( + 'title' => 'View', + 'type' => MENU_DEFAULT_LOCAL_TASK, + ); + $items['examples/entity_example/%entity_example/edit'] = array( + 'title' => 'Edit', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('entity_example_form_edit', 1), + 'access arguments' => array('administer example entity'), + 'type' => MENU_LOCAL_TASK, + 'weight' => 10, + ); + + $items['admin/structure/entity_example'] = array( + 'title' => 'Example entities', + 'description' => 'Manage example entities (Entity Example from Examples Project).', + 'access arguments' => array('administer example entity'), + 'page callback' => 'entity_example_page_admin', + 'page arguments' => array('list'), + 'weight' => -10, + ); + + $items['admin/structure/entity_example/list'] = array( + 'title' => 'List', + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'weight' => -10, + ); + + $items['admin/structure/entity_example/create'] = array( + 'title' => 'Add example entity', + 'page arguments' => array('create'), + 'access arguments' => array('administer example entity'), + 'type' => MENU_LOCAL_ACTION, + ); + + return $items; +} + +/** + * Implements hook_entity_info(). + * @todo: Explain much more about hook_entity_info and comment this. + */ +function entity_example_entity_info() { + $return = array( + 'entity_example' => array( + 'label' => t('Example entity'), + 'base table' => 'example_entity', + 'uri callback' => 'entity_example_uri', + 'fieldable' => TRUE, + 'entity keys' => array( + 'id' => 'eeid', + ), + 'bundles' => array( + 'entity_example' => array( + 'label' => t('Example entity'), + 'admin' => array( + 'path' => 'admin/structure/entity_example', + 'access arguments' => array('administer example entity'), + ), + ), + ), + ), + ); + return $return; +} + +/** + * Entity uri callback. + */ +function entity_example_uri($entity_example) { + return array( + 'path' => 'entity_example/' . $entity_example->eeid, + ); +} + +/** + * Implements hook_admin_paths(). + */ +function entity_example_admin_paths() { + $paths = array( + 'examples/entity_example/*/edit' => TRUE, + ); + return $paths; +} + +function entity_example_load($eeid, $reset = FALSE) { + $entity_examples = entity_example_load_multiple(array($eeid), array(), $reset); + return reset($entity_examples); +} + +function entity_example_load_multiple($eeids = array(), $conditions = array(), $reset = FALSE) { + return entity_load('entity_example', $eeids, $conditions, $reset); +} + +function entity_example_page_title($entity_example) { + return check_plain($entity_example->title); +} + +function entity_example_page($entity_example) { + // The module provides only one view mode. + $view_mode = 'default'; + + // Remove previously built content, if exists. + $entity_example->content = array(); + + // Build fields content. + field_attach_prepare_view('entity_example', array($entity_example->eeid => $entity_example), $view_mode); + entity_prepare_view('entity_example', array($entity_example->eeid => $entity_example)); + + $build = field_attach_view('entity_example', $entity_example, $view_mode); + return $build; +} + +/** + * Implements hook_field_extra_fields(). + */ +function entity_example_field_extra_fields() { + $return = array(); + $return['entity_example']['entity_example'] = array( + 'form' => array( + 'title' => array( + 'label' => t('Title'), + 'description' => t('Example entity module title form element'), + 'weight' => -10, + ), + 'perm' => array( + 'label' => t('Permanent mark'), + 'weight' => -9, + ), + 'dstart' => array( + 'label' => t('Start date'), + 'weight' => -8, + ), + 'dend' => array( + 'label' => t('End date'), + 'weight' => -7, + ), + 'weight' => array( + 'label' => t('Weight'), + 'weight' => -6, + ), + ), + ); + + return $return; +} + +function entity_example_save(&$edit) { + field_attach_presave('entity_example', $edit); + if (!empty($edit->eeid)) { + drupal_write_record('example_entity', $edit, 'eeid'); + field_attach_update('entity_example', $edit); + module_invoke_all('entity_update', 'entity_example', $edit); + return $edit; + } + drupal_write_record('example_entity', $edit); + field_attach_insert('entity_example', $edit); + module_invoke_all('entity_insert', 'entity_example', $edit); + return $edit; +} + +function entity_example_page_admin($tab = '') { + switch ($tab) { + case 'create': + $build['entity_example_create'] = drupal_get_form('entity_example_form_edit'); + break; + default: + $build['entity_example_list'] = drupal_get_form('entity_example_form_list'); + } + return $build; +} + +function entity_example_form_list() { + $header = array( + 'title' => array('data' => t('Title'), 'field' => 'ee.title'), + 'dstart' => array('data' => t('Start'), 'field' => 'dstart', 'sort' => 'asc'), + 'dend' => array('data' => t('End'), 'field' => 'dend'), + 'weight' => t('Weight'), + 'perm' => array('data' => t('Permanent'), 'field' => 'ee.perm'), + 'operations' => array('data' => t('Operations')), + ); + $query = db_select('example_entity', 'ee'); + $count_query = clone $query; + $count_query->addExpression('COUNT(ee.eeid)'); + + $query = $query->extend('PagerDefault')->extend('TableSort'); + $query + ->fields('ee', array('eeid', 'title', 'dstart', 'dend', 'perm', 'weight')) + ->limit(20) + ->orderByHeader($header) + ->setCountQuery($count_query); + $result = $query->execute(); + + $destination = drupal_get_destination(); + + $options = array(); + foreach ($result as $row) { + $options[$row->eeid] = array( + 'title' => array('data' => array( + '#type' => 'link', + '#title' => $row->title, + '#href' => "examples/entity_example/$row->eeid", + )), + 'dstart' => $row->dstart, + 'dend' => $row->dend, + 'perm' => array('data' => ($row->perm ? t('Yes') : t('No')), 'align' => 'center'), + 'weight' => $row->weight, + 'operations' => array('data' => array( + '#type' => 'link', + '#title' => t('edit'), + '#href' => "examples/entity_example/$row->eeid/edit", + '#options' => array('query' => $destination), + )), + ); + } + + $form['entity_examples'] = array( + '#type' => 'tableselect', + '#header' => $header, + '#options' => $options, + '#empty' => t('No entities available.'), + ); + $form['pager']['#markup'] = theme('pager'); + + return $form; +} + +function entity_example_form_edit($form, &$form_state, $edit = NULL) { + if (!isset($edit)) { + $edit = (object) array( + 'title' => '', + 'perm' => 0, + 'dstart' => date('Ymd'), + 'dend' => date('Ymd'), + 'weight' => 0, + ); + } + $form['perm'] = array( + '#type' => 'checkbox', + '#title' => t('Permanent'), + '#default_value' => $edit->perm, + ); + $form['title'] = array( + '#type' => 'textfield', + '#title' => t('Title'), + '#default_value' => $edit->title, + '#required' => TRUE, + ); + $form['dstart'] = array( + '#type' => 'date', + '#default_value' => _entity_example_to_array($edit->dstart), + '#title' => t('Start date'), + ); + $form['dend'] = array( + '#type' => 'date', + '#default_value' => _entity_example_to_array($edit->dend), + '#title' => t('End date'), + ); + $form['weight'] = array( + '#type' => 'weight', + '#default_value' => $edit->weight, + '#title' => t('Weight'), + ); + + // Attach fields from Field module. + field_attach_form('entity_example', (object) $edit, $form, $form_state); + + // Store ID if any. + if (!empty($edit->eeid)) { + $form['eeid'] = array( + '#type' => 'value', + '#value' => $edit->eeid, + ); + } + $form['actions'] = array('#type' => 'actions'); + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => t('Save'), + '#weight' => 5, + ); + + return $form; +} + +function entity_example_form_edit_validate($form, &$form_state) { + // Check dates. + $dstart = _entity_example_from_array($form_state['values']['dstart']); + $dend = _entity_example_from_array($form_state['values']['dend']); + if ($dstart > $dend) { + form_set_error('dend', t('End date must be later than start date.')); + } + // Attach validation from Field module. + field_attach_form_validate('entity_example', (object) $form_state['values'], $form, $form_state); +} + +function entity_example_form_edit_submit($form, &$form_state) { + $edit = (object) $form_state['values']; + // Attach submit handlers from Field module. + field_attach_submit('entity_example', $edit, $form, $form_state); + // Save own data. + $edit->dstart = _entity_example_from_array($edit->dstart); + $edit->dend = _entity_example_from_array($edit->dend); + entity_example_save($edit); + $form_state['redirect'] = "entity_example/$edit->eeid"; +} + +function _entity_example_to_array($date) { + $r = $matches = array(); + preg_match('/(\d{4})(\d{2})(\d{2})/', $date, $matches); + $r['year'] = (int) $matches[1]; + $r['month'] = (int) $matches[2]; + $r['day'] = (int) $matches[3]; + return $r; +} + +function _entity_example_from_array($array) { + return $array['year'] * 10000 + $array['month'] * 100 + $array['day']; +}