Need help in a module

No replies
sanasalim
Offline
Last seen: 4 years 2 weeks ago
Joined: 2015-02-11

Ive tried to develop a module that creates an entity artifact with two bundles department and course.Everytime I save a bundle, the message artifact saved is displayed, however 'Page Not Found' is displayed right underneath. Im not being able to figure why this is displayed. Can anyone please help me with it? Im attaching the code below:

escholar.info
core = '7.x'
description = 'Module Escholar for RCCIIT'
name = 'Escholar'
files[] = escholar.module
files[] = escholar.install
files[] = escholar.controller.inc

escholar.install
<?php

function escholar_schema() {
$schema['escholar'] = array(
'description' => 'The base table for artifacts.',
'fields' => array(
'aid' => array(
'description' => 'The primary identifier for an artifact.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'vid' => array(
'description' => 'The current {artifact_revision}.vid version identifier.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'type' => array(
'description' => 'The {artifact_type} of this artifact.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
),
'title' => array(
'description' => 'The title of this artifact.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'created' => array(
'description' => 'The Unix timestamp when the artifact was created.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'changed' => array(
'description' => 'The Unix timestamp when the artifact was most recently saved.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'unique keys' => array(
'aid_vid' => array('aid', 'vid'),
'aid' => array('aid')
),
'primary key' => array('aid'),
);

$schema['escholar_revision'] = array(
'description' => 'Stores information about each saved version of an {artifact}.',
'fields' => array(
'aid' => array(
'description' => 'The {artifact} this version belongs to.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'vid' => array(
'description' => 'The primary identifier for this version.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'title' => array(
'description' => 'The title of this version.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'created' => array(
'description' => 'The Unix timestamp when the artifact was created.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'indexes' => array(
'aid' => array('aid'),
),
'primary key' => array('vid'),
'foreign keys' => array(
'escholar ' => array(
'table' => 'escholar',
'columns' => array(
'aid' => 'aid',
),
),
),
);
return $schema;
}

escholar.module
<?php

/**
* Implements hook_entity_info().
*/

function escholar_entity_info() {
$items['escholar'] = array(
//specifies what name will be shown to user
'label' => t('Artifact'),
'controller class' => 'EscholarController',
'base table' => 'escholar',
'revision table' => 'escholar_revision',
'uri callback' => 'escholar_uri',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'aid',
'revision' => 'vid',
'bundle' => 'type',
'label' => 'title',
),
'bundle keys' => array(
'bundle' => 'type',
),
'static cache' => TRUE,
'bundles' => array(),
'view modes' => array(
'full' => array(
'label' => t('Full content'),
'custom settings' => FALSE,
),
'teaser' => array(
'label' => t('Teaser'),
'custom settings' => FALSE,
),
),
);

foreach (escholar_types() as $type => $info) {
$items['escholar']['bundles'][$type] = array(
'label' => $info->name,
'admin' => array(
'path' => 'admin/structure/escholar/manage/%escholar_type',
'real path' => 'admin/structure/escholar/manage/' . str_replace('_', '-', $type),
'bundle argument' => 4,
'access arguments' => array('administer artifacts'),
),
);
}

return $items;
}

function escholar_uri($escholar) {
return array(
'path' => 'escholar/' . $escholar->aid,
);
}

//returns a list of artifact type objects
function escholar_types() {
$types = &drupal_static(FUNCTION);
if (empty($types)) {
$types['department'] = (object)array(
'type' => 'department',
'name' => t('Department'),
'description' => t('Department at RCCIIT')
);

$types['course'] = (object)array(
'type' => 'course',
'name' => t('Course'),
'description' => t('The various courses at RCCIIT')
);
}

return $types;
}

function escholar_type_load($type) {
$types = escholar_types();
$type = str_replace('-', '_', $type);
return isset($types[$type]) ? $types[$type] : FALSE;
}

/**
* function escholar artifact menu()
*/
function escholar_menu() {
$items['escholar/%escholar/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('escholar_delete_confirm', 1),
'access arguments' => array('delete artifacts.'),
'weight' => 1,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
);

$items['escholar/%escholar/edit'] = array(
'title' => 'Edit',
'page callback' => 'escholar_page_edit',
'page arguments' => array(1),
'access arguments' => array('update artifacts'),
'weight' => 0,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);

$items['escholar/%escholar'] = array(
'title callback' => 'escholar_page_title',
'title arguments' => array(1),
'page callback' => 'escholar_page_view',
'page arguments' => array(1),
'access arguments' => array('view artifacts'),
'type' => MENU_CALLBACK,
);

$items['escholar/%escholar/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);

$items['escholar/add'] = array(
'title' => 'Add new artifact',
'page callback' => 'escholar_add_page',
'access arguments' => array('create artifacts'),
'weight' => 1,
'menu_name' => 'management',
'file' => 'escholar.pages.inc',
);

foreach (escholar_types() as $type) {
$type_url_str = str_replace('_', '-', $type->type);
$items['escholar/add/' . $type_url_str] = array(
'title' => $type->name,
//escapes any HTML content, making it safe to use as page title
'title callback' => 'check_plain',
'page callback' => 'escholar_add',
'page arguments' => array(2),
'access arguments' => array('create artifact'),
'description' => $type->description,
);
}

$items['admin/structure/escholar'] = array(
'title' => 'Manage artifacts',
'description' => 'Manage artifacts.',
'page callback' => 'escholar_overview_types',
'access arguments' => array('administer artifacts'),
);

$items['admin/structure/escholar/manage/%escholar_type'] = array(
'title' => 'View artifact type',
'title callback' => 'escholar_type_page_title',
'title arguments' => array(4),
'page callback' => 'escholar_information',
'page arguments' => array(4),
'access arguments' => array('administer artifacts'),
);

$items['admin/structure/escholar/manage/%escholar_type/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
);

return $items;
}

function escholar_overview_types() {
foreach (escholar_types() as $type => $info) {
$type_url_str = str_replace('_', '-', $type);
$label = t('View @type', array('@type' => $info->name));
$items[] = l($label, 'admin/structure/escholar/manage/' . $type_url_str);
}
return theme('item_list', array('items' => $items));
}

function escholar_type_page_title($type) {
return t('Manage @type', array('@type' => $type->name));
}

function escholar_information($escholar_type) {
return $escholar_type->name . ': ' . $escholar_type->description;
}

/**
* Generates a bulleted list of menu items that are immediate children of the current page being viewed.
*/

function escholar_add_page() {
//menu_get_item returns the current menu item
$item = menu_get_item();
//system_admin_menu_block is to get the children of an arbitrary menu item
$links = system_admin_menu_block($item);
foreach ($links as $link) {
//filter_xss_admin() strips out HTML tags that are unsafe
$items[] = l($link['title'], $link['href'], $item['localized_options']) . ': ' . filter_xss_admin($link['description']);
}
return theme('item_list', array('items' => $items));
}

function escholar_add($type) {
global $user;
$types = escholar_types();
$type = isset($type) ? str_replace('-', '_', $type) : NULL;
if (empty($types[$type])) {
return MENU_NOT_FOUND;
}
$escholar = entity_get_controller('escholar')->create($type);
drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)), PASS_THROUGH);
return drupal_get_form($type . '_escholar_form', $escholar);
}

/**
* hook_forms() dynamically defines forms and how they should behave
*/
function escholar_forms() {
$forms = array();
if ($types = escholar_types()) {
foreach (array_keys($types) as $type) {
$forms[$type . '_escholar_form']['callback'] = 'escholar_form';
}
}
return $forms;
}

/**
* hook_form() defines a new form for every artifact type and declares that all of them should use the same callback fucntion
*/
function escholar_form($form, &$form_state, $escholar) {
// Set the id to identify this as an artifact edit form.
$form['#id'] = 'escholar-form';
// Save the artifact for later, in case we need it.
$form['#escholar'] = $escholar;
$form_state['escholar'] = $escholar;
// Common fields.
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $escholar->title,
'#weight' => -5,
'#required' => TRUE,
);
$form['revision'] = array(
'#access' => user_access('administer artifacts'),
'#type' => 'checkbox',
'#title' => t('Create new revision'),
'#default_value' => 0,
);
// Add the buttons.
$form['buttons'] = array();
$form['buttons']['#weight'] = 100;
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 5,
'#submit' => array('escholar_form_submit'),
);
if (!empty($escholar->aid)) {
$form['buttons']['delete'] = array(
'#access' => user_access('delete artifacts'),
'#type' => 'submit',
'#value' => t('Delete'),
'#weight' => 15,
'#submit' => array('escholar_form_delete_submit'),
);
}
$form['#validate'][] = 'escholar_form_validate';
field_attach_form('escholar', $escholar, $form, $form_state);
return $form;
}

//validation callback
function escholar_form_validate($form, &$form_state) {
$escholar = $form_state['escholar'];

// Field validation.
field_attach_form_validate('escholar', $escholar, $form, $form_state);
}

function escholar_form_submit($form, &$form_state) {
global $user;
$escholar = &$form_state['escholar'];
// Set the artifact's uid if it's being created at this time.
if (empty($escholar->uid)) {
$escholar->uid = $user->uid;
}
$escholar->title = $form_state['values']['title'];
$escholar->revision = $form_state['values']['revision'];
// Notify field widgets.
field_attach_submit('escholar', $escholar, $form, $form_state);
// Save the artifact.
escholar_save($escholar);
// Notify the user.
drupal_set_message(t('Artifact saved.'));
$form_state['redirect'] = 'escholar/' . $escholar->aid;
}

function escholar_save($escholar) {
return entity_get_controller('escholar')->save($escholar);
}

function escholar_page_title($escholar) {
return $escholar->title;
}

function escholar_page_view($escholar, $view_mode = 'full') {
// Remove previously built content, if exists.
$escholar->content = array();
if ($view_mode == 'teaser') {
$escholar->content['title'] = array(
'#markup' => filter_xss($escholar->title),
'#weight' => -5,
);
}
// Build fields content.
field_attach_prepare_view('escholar', array($escholar->aid => $escholar), $view_mode);
entity_prepare_view('escholar', array($escholar->aid => $escholar));
$escholar->content += field_attach_view('escholar', $escholar, $view_mode);
return $escholar->content;
}

function escholar_page_edit($escholar) {
$types = escholar_types();
drupal_set_title(t('Edit @type @title', array('@type' => $types[$escholar->type]->name, '@title' => $escholar->title)), PASS_THROUGH);
return drupal_get_form($escholar->type . '_escholar_form', $escholar);
}

function escholar_form_delete_submit($form, &$form_state) {
$destination = array();
if (isset($_GET['destination'])) {
$destination = drupal_get_destination();
unset($_GET['destination']);
}
$escholar = $form['#escholar'];
$form_state['redirect'] = array('escholar/' . $escholar->aid . '/delete', array('query' => $destination));
}

function escholar_delete_confirm($form, &$form_state, $escholar) {
$form['#escholar'] = $escholar;
// The entity id must be provided in the same form key as in the entity edit form.
$form['aid'] = array('#type' => 'value', '#value' => $escholar->aid);
return confirm_form($form, t('Are you sure you want to delete %title?', array('%title' => $escholar->title)), 'escholar/' . $escholar->aid, t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}

function escholar_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$escholar = escholar_load($form_state['values']['aid']);
escholar_delete($form_state['values']['aid']);
watchdog('escholar', '@type: deleted %title.', array('@type' => $escholar->type, '%title' => $escholar->title));
$types = escholar_types();
drupal_set_message(t('@type %title has been deleted.', array('@type' => $types[$escholar->type]->name, '%title' => $escholar->title)));
}
$form_state['redirect'] = '';
}

function escholar_delete($aid) {
return escholar_delete_multiple(array($aid));
}

function escholar_delete_multiple($aids) {
return entity_get_controller('escholar')->delete($aids);
}

escholar.controller.inc
<?php

class EscholarController extends DrupalDefaultEntityController {

function escholar_load($aid = NULL, $vid = NULL, $reset = FALSE) {
$aids = (isset($aid) ? array($aid) : array());
$conditions = (isset($vid) ? array('vid' => $vid) : array());
$escholar = escholar_load_multiple($aids, $conditions, $reset);
return $escholar ? reset($escholar) : FALSE;

}

/**
* Loads multiple basic entities.
* We only need to pass this request along to entity_load(), which
* will in turn call the load() method of our entity controller class.
*/
function escholar_load_multiple($aids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('escholar', $aids, $conditions, $reset);
}

function saveRevision($escholar, $uid, $update = FALSE) {
// Hold on to the artifact's original creator_uid but swap
// in the revision's creator_uid for the momentary write.
$temp_uid = $escholar->uid;
$escholar->uid = $uid;
if ($update) {
drupal_write_record('escholar_revision', $escholar, 'vid');
}
else {
drupal_write_record('escholar_revision', $escholar);
}
// Reset the order's creator_uid to the original value.
$escholar->uid = $temp_uid;
}

public function create($type = '') {
return (object) array(
'aid' => '',
'type' => $type,
'title' => '',
);
}

public function save($escholar) {
$transaction = db_transaction();
try {
global $user;
// Determine if we will be inserting a new escholar.
$escholar->is_new = empty($escholar->aid);
// Set the timestamp fields.
if (empty($escholar->created)) {
$escholar->created = REQUEST_TIME;
}
$escholar->changed = REQUEST_TIME;
$escholar->revision_timestamp = REQUEST_TIME;
$update_escholar = TRUE;
// Give modules the opportunity to prepare field data for saving.
field_attach_presave('escholar', $escholar);
if (!$escholar->is_new && !empty($escholar->revision) && $escholar->vid) {
$escholar->old_vid = $escholar->vid;
unset($escholar->vid);
}
// If this is a new artifact
if ($escholar->is_new) {
// Save the new artifact.
drupal_write_record('escholar', $escholar);
// Save the initial revision.
$this->saveRevision($escholar, $user->uid);
$op = 'insert';
}
else {
// Save the updated artifact.
drupal_write_record('escholar', $escholar, 'aid');
if (!empty($escholar->revision)) {
EscholarController::saveRevision();
$this->saveRevision($escholar, $user->uid);
}
else {
$this->saveRevision($escholar, $user->uid, TRUE);
$update_escholar = FALSE;
}
$op = 'update';
}
// If the revision ID is new or updated, save it to the artifact.
if ($update_escholar) {
db_update('escholar')
->fields(array('vid' => $escholar->vid))
->condition('aid', $escholar->aid)
->execute();
}
// Save fields.
$function = 'field_attach_' . $op;
$function('escholar', $escholar);
module_invoke_all('entity_' . $op, $escholar, 'escholar');
// Clear internal properties.
unset($escholar->is_new);
// Ignore slave server temporarily to give time for the saved
// order to be propagated to the slave.
db_ignore_slave();
return $escholar;
}

catch (Exception $e) {
$transaction->rollback();
watchdog_exception('escholar', $e, NULL, WATCHDOG_ERROR);
return FALSE;
}

}

public function delete($aids) {
if (!empty($aids)) {
$escholars = $this->load($aids, array());
$transaction = db_transaction();
try {
db_delete('escholar')
->condition('aid', $aids, 'IN')
->execute();
db_delete('escholar_revision')
->condition('aid', $aids, 'IN')
->execute();
foreach ($escholars as $escholar_id => $escholar) {
field_attach_delete('escholar', $escholar);
}
// Ignore slave server temporarily to give time for the saved
// order to be propagated to the slave.
db_ignore_slave();
}
catch (Exception $e) {
$transaction->rollback();
watchdog_exception('escholar', $e, NULL, WATCHDOG_ERROR);
return FALSE;
}
module_invoke_all('entity_delete', $escholar, 'escholar');
// Clear the page, block and escholar caches.
cache_clear_all();
$this->resetCache();
}
return TRUE;
}
}

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.