0

I have created a table 'Challenge'. It stores node id of some particular nodes along with the user who clicked a submit button on that page. I have written a custom form for that and insert query which works fine as i can check from my phpMyadmin. Now i want to create a view to display the user their challenges. That view should have the challenge name(in essence the node title), the challenge body in trimmed format(in essence the node body), the challenge picture(in essence the challenge picture). The 'Challenge' table has challenge id,cid (which is node->id), challenge title,ctitle (which is node->title), and the user id,uid. This insertion is happening as required. The view creation is the problem. I tried to do so with the views ui but was not able to do so. Hence i tried to custom code it. I write a custom module and it installs but when i go under structure/admin/view i do not see it. My use case is that the user should be able to see the challenges he took (nodes on which he clicked the accept button).

Here's my code

customv.info

name = Custom View
description = A custom view to display users and their challenges 
core = 7.x

customv.module

<?php


function customv_help($path, $arg) {
  switch ($path) {
    case "admin/help#customv":
      return '<p>' . t("View for user and challenge") . '</p>';
      break;
  }
}

/**
 * Implements hook_views_api().
 */

function customv_views_api() {
  return array(
    'api' => '3.0',
    'path' => drupal_get_path('module', 'customv'),
  );
}

/**
 * Implements hook_views_default_views().
 */
function customv_views_default_views() {
  return customv_get_chal();
}

function customv_get_chal() {
    $view = new view();
    $view->name = 'user_chal';
    $view->description = 'A custom view to display users and their challenges';
    $view->tag = 'default';
    $view->base_table = 'challenges';
    $view->human_name = 'User Challenges';
    $view->core = 7;
    $view->api_version = '3.0';
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

    /* 
     * Display: Master 
     */

    $handler = $view->new_display('default', 'Master', 'default');
    $handler->display->display_options['title'] = 'User Challenges';
    $handler->display->display_options['use_more_always'] = FALSE;
    $handler->display->display_options['access']['type'] = 'perm';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'some';
    $handler->display->display_options['pager']['options']['items_per_page'] = '10';
    $handler->display->display_options['style_plugin'] = 'grid';
    $handler->display->display_options['style_options']['columns'] = '5';
    $handler->display->display_options['row_plugin'] = 'fields';

    /* 
     *Field: Content: Title 
     */

    $handler->display->display_options['fields']['title']['id'] = 'ctitle';
    $handler->display->display_options['fields']['title']['table'] = 'challenges';
    $handler->display->display_options['fields']['title']['field'] = 'ctitle';
    $handler->display->display_options['fields']['title']['label'] = '';
    $handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
    $handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;

    /* 
    Field: Content: Body 
    */

    $handler->display->display_options['fields']['body']['id'] = 'body';
    $handler->display->display_options['fields']['body']['table'] = 'field_data_body';
    $handler->display->display_options['fields']['body']['field'] = 'body';
    $handler->display->display_options['fields']['body']['label'] = '';
    $handler->display->display_options['fields']['body']['element_label_colon'] = FALSE;
    $handler->display->display_options['fields']['body']['type'] = 'text_summary_or_trimmed';
    $handler->display->display_options['fields']['body']['settings'] = array(
      'trim_length' => '200',
    );

    /* 
     *Field: Content: blog_pic 
     */

    $handler->display->display_options['fields']['field_blog_pic']['id'] = 'field_challenge_pic';
    $handler->display->display_options['fields']['field_blog_pic']['table'] = 'field_data_field_challenge_pic';
    $handler->display->display_options['fields']['field_blog_pic']['field'] = 'field_challenge_pic';
    $handler->display->display_options['fields']['field_blog_pic']['label'] = '';
    $handler->display->display_options['fields']['field_blog_pic']['element_label_colon'] = FALSE;
    $handler->display->display_options['fields']['field_blog_pic']['click_sort_column'] = 'fid';
    $handler->display->display_options['fields']['field_blog_pic']['settings'] = array(
      'image_style' => '',
      'image_link' => 'content',
    );
    $handler->display->display_options['fields']['field_challenge_pic']['delta_offset'] = '0';

    /* 
     *Contextual filter: Content: Author uid 
     */

    $handler->display->display_options['arguments']['uid']['id'] = 'uid';
    $handler->display->display_options['arguments']['uid']['table'] = 'challenges';
    $handler->display->display_options['arguments']['uid']['field'] = 'uid';
    $handler->display->display_options['arguments']['uid']['default_action'] = 'default';
    $handler->display->display_options['arguments']['uid']['default_argument_type'] = 'current_user';
    $handler->display->display_options['arguments']['uid']['summary']['number_of_records'] = '0';
    $handler->display->display_options['arguments']['uid']['summary']['format'] = 'default_summary';
    $handler->display->display_options['arguments']['uid']['summary_options']['items_per_page'] = '25';

    /* 
     *Display: Block 
     */

    $handler = $view->new_display('block', 'Block', 'block_1');
    $handler->display->display_options['defaults']['hide_admin_links'] = FALSE;

}

SO what am i doing wrong here???

Thanks for your time because its a long question to read...sorry for that...would really appreciate some help...and yes i used view exporter to import code from an already created view (show content created by the logged in user only) and tweaked the code

1

Your customv_get_chal() function doesn't return any value. As consequence of this, your hook is not returning any view to the Views module.

The correct code should be the following one.

function customv_get_chal() {
  $views = array();

  // The rest of your code

  $views['user_chal'] = $view;

  return $views;
}
  • thanks...yes i also figured it out...however now when i try to see that view i am getting an error: Undefined index: table in view->fix_missing_relationships() (line 617 of /opt/lampp/htdocs/sites/all/modules/views/includes/view.inc). – why Feb 12 '13 at 3:20
  • i went and checked that file however i am not sure...will keep looking....thanks again – why Feb 12 '13 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.