1

I have a Drupal setup that is currently having a question and answers module.

I am able to read the question and answers by accessing http://mysite.tld/rest_endpoint/node/{nodeid} . However when I access a question, I am not able to get the nodeIds of all the answers to that question.

So I am looking to get the Node Ids for all the answers for a specific question.

  • What params/filters do I need to use in the Drupal Services GET call? I have services module.

  • Can I achieve this by doing a database query? I am not able to find a link between questions and answers in the DB too!

Any suggestions?

4

I didn't know what are question and answers modules that you used. My assumption is your answer and your question has a content type and they linked together. So if my assumption is correct here is the way that you can use for solving your problems:

  1. Create a views will list all the answers of specific question(your question nid).
  2. using Views Services to get the views that you created before and use your question (question nid) for the views parameter.

In case my assumption is wrong. Your have to create custom resource by your own to get the list of answers. In this way you must create your SQL sentences too. The reference for creating custom resource here

  • Thanks for your answer. Your assumption is correct. However as i am restricted on installign new modules, I was able to figure out the Mysql table for question/ answer references. I did a join with field_data_field_reference and node table to achieve this. – Eswar Rajesh Pinapala Nov 15 '13 at 20:35
0

Have a look at this (exported) custom view related to the Answers module, which lists all existing answers for any of the existing questions:

$view = new view();
$view->name = 'answers';
$view->description = '';
$view->tag = 'answers, answers_best_answer, answers_userpoints, answers_voting';
$view->base_table = 'node';
$view->human_name = 'Answers';
$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'] = 'All Answers';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['group_by'] = TRUE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '50';
$handler->display->display_options['style_plugin'] = 'datatables';
$handler->display->display_options['style_options']['columns'] = array(
  'title' => 'title',
  'name' => 'name',
  'value' => 'value',
  'flagged' => 'flagged',
  'nid' => 'nid',
  'created' => 'created',
  'changed' => 'changed',
  'new_answers' => 'new_answers',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
  'title' => array(
    'sortable' => 1,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'name' => array(
    'sortable' => 1,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'value' => array(
    'sortable' => 1,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'flagged' => array(
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'nid' => array(
    'sortable' => 1,
    'default_sort_order' => 'desc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'created' => array(
    'sortable' => 1,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'changed' => array(
    'sortable' => 1,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'new_answers' => array(
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
);
$handler->display->display_options['style_options']['elements'] = array(
  'search_box' => 1,
  'table_info' => 1,
  'save_state' => 1,
  'table_tools' => 0,
);
$handler->display->display_options['style_options']['layout'] = array(
  'autowidth' => 0,
  'themeroller' => 0,
  'sdom' => '',
);
$handler->display->display_options['style_options']['pages'] = array(
  'pagination_style' => 'full_numbers',
  'length_change' => 1,
  'display_length' => '25',
);
/* No results behavior: Global: Text area */
$handler->display->display_options['empty']['area']['id'] = 'area';
$handler->display->display_options['empty']['area']['table'] = 'views';
$handler->display->display_options['empty']['area']['field'] = 'area';
$handler->display->display_options['empty']['area']['label'] = 'No Questions Blurb';
$handler->display->display_options['empty']['area']['empty'] = TRUE;
$handler->display->display_options['empty']['area']['content'] = '<em>No questions have been entered yet</em>';
$handler->display->display_options['empty']['area']['format'] = 'filtered_html';
/* Relationship: Userpoints total: User */
$handler->display->display_options['relationships']['uid_1']['id'] = 'uid_1';
$handler->display->display_options['relationships']['uid_1']['table'] = 'userpoints_total';
$handler->display->display_options['relationships']['uid_1']['field'] = 'uid';
/* Relationship: Content: Vote results */
$handler->display->display_options['relationships']['votingapi_cache']['id'] = 'votingapi_cache';
$handler->display->display_options['relationships']['votingapi_cache']['table'] = 'node';
$handler->display->display_options['relationships']['votingapi_cache']['field'] = 'votingapi_cache';
$handler->display->display_options['relationships']['votingapi_cache']['votingapi'] = array(
  'value_type' => 'points',
  'tag' => '',
  'function' => 'sum',
);
/* Relationship: Content: Author */
$handler->display->display_options['relationships']['uid']['id'] = 'uid';
$handler->display->display_options['relationships']['uid']['table'] = 'node';
$handler->display->display_options['relationships']['uid']['field'] = 'uid';
/* Relationship: Entity Reference: Referenced Entity */
$handler->display->display_options['relationships']['answers_related_question_target_id']['id'] = 'answers_related_question_target_id';
$handler->display->display_options['relationships']['answers_related_question_target_id']['table'] = 'field_data_answers_related_question';
$handler->display->display_options['relationships']['answers_related_question_target_id']['field'] = 'answers_related_question_target_id';
$handler->display->display_options['relationships']['answers_related_question_target_id']['required'] = TRUE;
/* Relationship: Flags: best_answer */
$handler->display->display_options['relationships']['flag_content_rel']['id'] = 'flag_content_rel';
$handler->display->display_options['relationships']['flag_content_rel']['table'] = 'node';
$handler->display->display_options['relationships']['flag_content_rel']['field'] = 'flag_content_rel';
$handler->display->display_options['relationships']['flag_content_rel']['required'] = 0;
$handler->display->display_options['relationships']['flag_content_rel']['flag'] = 'best_answer';
$handler->display->display_options['relationships']['flag_content_rel']['user_scope'] = 'any';
/* Relationship: Userpoints total: User */
$handler->display->display_options['relationships']['uid_2']['id'] = 'uid_2';
$handler->display->display_options['relationships']['uid_2']['table'] = 'userpoints_total';
$handler->display->display_options['relationships']['uid_2']['field'] = 'uid';
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['relationship'] = 'answers_related_question_target_id';
$handler->display->display_options['fields']['title']['label'] = 'Question';
/* Field: Userpoints total: Current total reputation */
$handler->display->display_options['fields']['points']['id'] = 'points';
$handler->display->display_options['fields']['points']['table'] = 'userpoints_total';
$handler->display->display_options['fields']['points']['field'] = 'points';
$handler->display->display_options['fields']['points']['exclude'] = TRUE;
/* Field: User: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'users';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['relationship'] = 'uid';
$handler->display->display_options['fields']['name']['label'] = 'Answered by';
$handler->display->display_options['fields']['name']['alter']['alter_text'] = TRUE;
$handler->display->display_options['fields']['name']['alter']['text'] = '[name]<br /><span class="answers-views-userpoints">([points])</span>';
$handler->display->display_options['fields']['name']['element_label_colon'] = FALSE;
/* Field: Vote results: Value */
$handler->display->display_options['fields']['value']['id'] = 'value';
$handler->display->display_options['fields']['value']['table'] = 'votingapi_cache';
$handler->display->display_options['fields']['value']['field'] = 'value';
$handler->display->display_options['fields']['value']['relationship'] = 'votingapi_cache';
$handler->display->display_options['fields']['value']['label'] = 'V';
$handler->display->display_options['fields']['value']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['value']['precision'] = '0';
/* Field: COUNT(Flags: Flagged) */
$handler->display->display_options['fields']['flagged']['id'] = 'flagged';
$handler->display->display_options['fields']['flagged']['table'] = 'flagging';
$handler->display->display_options['fields']['flagged']['field'] = 'flagged';
$handler->display->display_options['fields']['flagged']['relationship'] = 'flag_content_rel';
$handler->display->display_options['fields']['flagged']['group_type'] = 'count';
$handler->display->display_options['fields']['flagged']['label'] = '';
$handler->display->display_options['fields']['flagged']['alter']['alter_text'] = TRUE;
$handler->display->display_options['fields']['flagged']['alter']['text'] = '<img src="/misc/message-16-ok.png" />';
$handler->display->display_options['fields']['flagged']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['flagged']['empty_zero'] = TRUE;
/* Field: Content: Nid */
$handler->display->display_options['fields']['nid']['id'] = 'nid';
$handler->display->display_options['fields']['nid']['table'] = 'node';
$handler->display->display_options['fields']['nid']['field'] = 'nid';
$handler->display->display_options['fields']['nid']['label'] = 'Answer';
$handler->display->display_options['fields']['nid']['link_to_node'] = TRUE;
/* Field: Content: Post date */
$handler->display->display_options['fields']['created']['id'] = 'created';
$handler->display->display_options['fields']['created']['table'] = 'node';
$handler->display->display_options['fields']['created']['field'] = 'created';
$handler->display->display_options['fields']['created']['label'] = 'Created';
$handler->display->display_options['fields']['created']['date_format'] = 'short';
$handler->display->display_options['fields']['created']['second_date_format'] = 'long';
/* Field: Content: Updated date */
$handler->display->display_options['fields']['changed']['id'] = 'changed';
$handler->display->display_options['fields']['changed']['table'] = 'node';
$handler->display->display_options['fields']['changed']['field'] = 'changed';
$handler->display->display_options['fields']['changed']['label'] = 'Updated';
$handler->display->display_options['fields']['changed']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['changed']['date_format'] = 'short';
/* Field: Answers: Has new content */
$handler->display->display_options['fields']['new_answers']['id'] = 'new_answers';
$handler->display->display_options['fields']['new_answers']['table'] = 'answers';
$handler->display->display_options['fields']['new_answers']['field'] = 'new_answers';
$handler->display->display_options['fields']['new_answers']['label'] = 'New';
/* Sort criterion: Content: Nid */
$handler->display->display_options['sorts']['nid']['id'] = 'nid';
$handler->display->display_options['sorts']['nid']['table'] = 'node';
$handler->display->display_options['sorts']['nid']['field'] = 'nid';
$handler->display->display_options['sorts']['nid']['order'] = 'DESC';
/* Contextual filter: Content: Author uid */
$handler->display->display_options['arguments']['uid']['id'] = 'uid';
$handler->display->display_options['arguments']['uid']['table'] = 'node';
$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';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'answers_answer' => 'answers_answer',
);
$handler->display->display_options['filters']['type']['group'] = 1;

/* Display: Answers */
$handler = $view->new_display('page', 'Answers', 'page');
$handler->display->display_options['display_description'] = 'List of all answers';
$handler->display->display_options['defaults']['arguments'] = FALSE;
$handler->display->display_options['path'] = 'answers';
$handler->display->display_options['menu']['type'] = 'normal';
$handler->display->display_options['menu']['title'] = 'Answers';
$handler->display->display_options['menu']['description'] = 'All Answers';
$handler->display->display_options['menu']['weight'] = '1';
$handler->display->display_options['menu']['name'] = 'menu-answers-menu';
$handler->display->display_options['menu']['context'] = 0;
$handler->display->display_options['menu']['context_only_inline'] = 0;
$handler->display->display_options['tab_options']['weight'] = '0';

/* Display: Answers by User */
$handler = $view->new_display('page', 'Answers by User', 'page_1');
$handler->display->display_options['defaults']['title'] = FALSE;
$handler->display->display_options['title'] = 'Answers by author (uid = %1)';
$handler->display->display_options['display_description'] = 'List of all answers from a specific user';
$handler->display->display_options['defaults']['style_plugin'] = FALSE;
$handler->display->display_options['style_plugin'] = 'datatables';
$handler->display->display_options['style_options']['columns'] = array(
  'title' => 'title',
  'value' => 'value',
  'flagged' => 'flagged',
  'nid' => 'nid',
  'created' => 'created',
  'changed' => 'changed',
  'new_answers' => 'new_answers',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
  'title' => array(
    'sortable' => 1,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'value' => array(
    'sortable' => 1,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'flagged' => array(
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'nid' => array(
    'sortable' => 1,
    'default_sort_order' => 'desc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'created' => array(
    'sortable' => 1,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'changed' => array(
    'sortable' => 1,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'new_answers' => array(
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
);
$handler->display->display_options['style_options']['elements'] = array(
  'search_box' => 1,
  'table_info' => 1,
  'save_state' => 1,
  'table_tools' => 0,
);
$handler->display->display_options['style_options']['layout'] = array(
  'autowidth' => 0,
  'themeroller' => 0,
  'sdom' => '',
);
$handler->display->display_options['style_options']['pages'] = array(
  'pagination_style' => 'full_numbers',
  'length_change' => 1,
  'display_length' => '25',
);
$handler->display->display_options['defaults']['style_options'] = FALSE;
$handler->display->display_options['defaults']['row_plugin'] = FALSE;
$handler->display->display_options['defaults']['row_options'] = FALSE;
$handler->display->display_options['defaults']['fields'] = FALSE;
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['relationship'] = 'answers_related_question_target_id';
$handler->display->display_options['fields']['title']['label'] = 'Question';
/* Field: Vote results: Value */
$handler->display->display_options['fields']['value']['id'] = 'value';
$handler->display->display_options['fields']['value']['table'] = 'votingapi_cache';
$handler->display->display_options['fields']['value']['field'] = 'value';
$handler->display->display_options['fields']['value']['relationship'] = 'votingapi_cache';
$handler->display->display_options['fields']['value']['label'] = 'V';
$handler->display->display_options['fields']['value']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['value']['precision'] = '0';
/* Field: COUNT(Flags: Flagged) */
$handler->display->display_options['fields']['flagged']['id'] = 'flagged';
$handler->display->display_options['fields']['flagged']['table'] = 'flagging';
$handler->display->display_options['fields']['flagged']['field'] = 'flagged';
$handler->display->display_options['fields']['flagged']['relationship'] = 'flag_content_rel';
$handler->display->display_options['fields']['flagged']['group_type'] = 'count';
$handler->display->display_options['fields']['flagged']['label'] = '';
$handler->display->display_options['fields']['flagged']['alter']['alter_text'] = TRUE;
$handler->display->display_options['fields']['flagged']['alter']['text'] = '<img src="/misc/message-16-ok.png" />';
$handler->display->display_options['fields']['flagged']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['flagged']['empty_zero'] = TRUE;
/* Field: Content: Nid */
$handler->display->display_options['fields']['nid']['id'] = 'nid';
$handler->display->display_options['fields']['nid']['table'] = 'node';
$handler->display->display_options['fields']['nid']['field'] = 'nid';
$handler->display->display_options['fields']['nid']['label'] = 'Answer';
$handler->display->display_options['fields']['nid']['link_to_node'] = TRUE;
/* Field: Content: Post date */
$handler->display->display_options['fields']['created']['id'] = 'created';
$handler->display->display_options['fields']['created']['table'] = 'node';
$handler->display->display_options['fields']['created']['field'] = 'created';
$handler->display->display_options['fields']['created']['label'] = 'Created';
$handler->display->display_options['fields']['created']['date_format'] = 'short';
$handler->display->display_options['fields']['created']['second_date_format'] = 'long';
/* Field: Content: Updated date */
$handler->display->display_options['fields']['changed']['id'] = 'changed';
$handler->display->display_options['fields']['changed']['table'] = 'node';
$handler->display->display_options['fields']['changed']['field'] = 'changed';
$handler->display->display_options['fields']['changed']['label'] = 'Updated';
$handler->display->display_options['fields']['changed']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['changed']['date_format'] = 'short';
/* Field: Answers: Has new content */
$handler->display->display_options['fields']['new_answers']['id'] = 'new_answers';
$handler->display->display_options['fields']['new_answers']['table'] = 'answers';
$handler->display->display_options['fields']['new_answers']['field'] = 'new_answers';
$handler->display->display_options['fields']['new_answers']['label'] = 'New';
$handler->display->display_options['path'] = 'user/%/answers';
$handler->display->display_options['menu']['type'] = 'tab';
$handler->display->display_options['menu']['title'] = 'Answers';
$handler->display->display_options['menu']['description'] = 'Answers';
$handler->display->display_options['menu']['weight'] = '5';
$handler->display->display_options['menu']['name'] = 'user-menu';
$handler->display->display_options['menu']['context'] = 0;
$handler->display->display_options['menu']['context_only_inline'] = 0;
$handler->display->display_options['tab_options']['weight'] = '0';

You can see it at work in the demo site of the Answers module. It uses the dataTables format, but you could easily replace that with any views display format you'd want.

By using this view, you should be able to:

  • tune the fields, sorting, etc to fit your needs.
  • addng a filter for the question for which you want to retrieve all answers.
  • use the Services Views module to get the nodeIds of all the answers to that question.

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.