book_parent.inc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide an relationship handler for book parent.
  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('Book parent'),
  12. 'keyword' => 'book_parent',
  13. 'description' => t('Adds a book parent from a node context.'),
  14. 'required context' => new ctools_context_required(t('Node'), 'node'),
  15. 'context' => 'ctools_book_parent_context',
  16. 'edit form' => 'ctools_book_parent_settings_form',
  17. 'defaults' => array('type' => 'top'),
  18. );
  19. /**
  20. * Return a new context based on an existing context.
  21. */
  22. function ctools_book_parent_context($context, $conf) {
  23. // If unset it wants a generic, unfilled context, which is just NULL.
  24. if (empty($context->data)) {
  25. return ctools_context_create_empty('node');
  26. }
  27. if (isset($context->data->book)) {
  28. if ($conf['type'] == 'top') {
  29. $nid = $context->data->book['bid'];
  30. }
  31. else {
  32. // Just load the parent book.
  33. $item = book_link_load($context->data->book['plid']);
  34. $nid = $item['nid'];
  35. }
  36. if (!empty($nid)) {
  37. // Load the node.
  38. $node = node_load($nid);
  39. // Generate the context.
  40. if (node_access('view', $node)) {
  41. return ctools_context_create('node', $node);
  42. }
  43. }
  44. }
  45. else {
  46. return ctools_context_create_empty('node');
  47. }
  48. }
  49. /**
  50. * Settings form for the relationship.
  51. */
  52. function ctools_book_parent_settings_form($form, &$form_state) {
  53. $conf = $form_state['conf'];
  54. $form['type'] = array(
  55. '#type' => 'select',
  56. '#title' => t('Relationship type'),
  57. '#options' => array('parent' => t('Immediate parent'), 'top' => t('Top level book')),
  58. '#default_value' => $conf['type'],
  59. );
  60. return $form;
  61. }