node_from_view.inc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide an relationship handler for node from view.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => t('Node from view'),
  12. 'keyword' => 'node',
  13. 'description' => t('Extract a node context from a view context of the base type node.'),
  14. 'required context' => new ctools_context_required(t('View'), 'view', array('base' => 'node')),
  15. 'context' => 'views_content_node_from_view_context',
  16. 'edit form' => 'views_content_node_from_view_settings_form',
  17. 'edit form validate' => 'views_content_node_from_view_settings_form_validate',
  18. 'defaults' => array('row' => 1),
  19. );
  20. /**
  21. * Return a new context based on an existing context.
  22. */
  23. function views_content_node_from_view_context($context, $conf, $placeholder = FALSE) {
  24. // If unset it wants a generic, unfilled context, which is just NULL.
  25. if (empty($context->data) || $placeholder) {
  26. return ctools_context_create_empty('node', NULL);
  27. }
  28. $view = views_content_context_get_view($context);
  29. // Ensure the view executes, but we don't need its output.
  30. views_content_context_get_output($context);
  31. $row = intval($conf['row']) - 1;
  32. if (isset($view->result[$row]) && isset($view->base_field) && isset($view->result[$row]->{$view->base_field})) {
  33. $nid = $view->result[$row]->{$view->base_field};
  34. if ($nid) {
  35. $node = node_load($nid);
  36. return ctools_context_create('node', $node);
  37. }
  38. }
  39. return ctools_context_create_empty('node', NULL);
  40. }
  41. /**
  42. * Settings form for the relationship.
  43. */
  44. function views_content_node_from_view_settings_form($form, &$form_state) {
  45. $conf = $form_state['conf'];
  46. $form['row'] = array(
  47. '#title' => t('Row number'),
  48. '#type' => 'textfield',
  49. '#default_value' => $conf['row'],
  50. );
  51. return $form;
  52. }
  53. function views_content_node_from_view_settings_form_validate($form, &$form_state) {
  54. if (intval($form_state['values']['row']) <= 0) {
  55. form_error($form['row'], t('Row number must be a positive integer value.'));
  56. }
  57. }