book_parent.inc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. return ctools_context_create('node', $node);
  41. }
  42. }
  43. else {
  44. return ctools_context_create_empty('node');
  45. }
  46. }
  47. /**
  48. * Settings form for the relationship.
  49. */
  50. function ctools_book_parent_settings_form($form, &$form_state) {
  51. $conf = $form_state['conf'];
  52. $form['type'] = array(
  53. '#type' => 'select',
  54. '#title' => t('Relationship type'),
  55. '#options' => array('parent' => t('Immediate parent'), 'top' => t('Top level book')),
  56. '#default_value' => $conf['type'],
  57. );
  58. return $form;
  59. }