contextual.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @file
  4. * Contextual links module integration.
  5. */
  6. /**
  7. * Implements hook_custom_formatters_form_alter_alter() on behalf of
  8. * contextual.module.
  9. */
  10. function contextual_custom_formatters_form_alter_alter(&$form, $form_state, $form_id) {
  11. if ($form_id == 'custom_formatters_settings_form') {
  12. $settings = variable_get('custom_formatters_settings', array('contextual' => 1));
  13. $form['settings']['contextual'] = array(
  14. '#type' => 'radios',
  15. '#title' => t('Contextual links integration'),
  16. '#default_value' => isset($settings['contextual']) ? $settings['contextual'] : 1,
  17. '#options' => array(
  18. 0 => t('Disabled'),
  19. 1 => t('Enabled on all Formatters except those listed'),
  20. 2 => t('Enabled on only the listed Formatters'),
  21. ),
  22. );
  23. $form['settings']['contextual_list'] = array(
  24. '#type' => 'textarea',
  25. '#default_value' => isset($settings['contextual_list']) ? $settings['contextual_list'] : '',
  26. '#description' => t('Specify Formatters by using their machine names. Enter one machine name per line.'),
  27. '#states' => array(
  28. 'visible' => array(
  29. 'input[name="settings[contextual]"]' => array('checked' => FALSE),
  30. ),
  31. ),
  32. );
  33. }
  34. }
  35. /**
  36. * Implements hook_custom_formatters_field_formatter_view_element_alter() on
  37. * behalf of contextual.module.
  38. *
  39. * Adds contextual links to Custom Formatter fields.
  40. */
  41. function contextual_custom_formatters_field_formatter_view_element_alter(&$element, $formatter) {
  42. if (_custom_formatters_contextual_access($formatter->name, $element)) {
  43. foreach (element_children($element) as $delta) {
  44. $element[$delta] = array(
  45. 'markup' => $element[$delta],
  46. 'contextual_links' => array(
  47. '#type' => 'contextual_links',
  48. '#contextual_links' => array('custom_formatters' => array('admin/structure/formatters/list', array($formatter->name, 'edit'))),
  49. '#element' => $element[$delta],
  50. ),
  51. '#prefix' => '<div class="contextual-links-region">',
  52. '#suffix' => '</div>',
  53. );
  54. }
  55. }
  56. }
  57. function _custom_formatters_contextual_access($name, $element) {
  58. if (isset($element[0]['#cf_options']['#contextual_links']) && $element[0]['#cf_options']['#contextual_links'] == FALSE) {
  59. return FALSE;
  60. }
  61. $user_access = user_access('access contextual links') && user_access('administer custom formatters');
  62. if (!$user_access) {
  63. return FALSE;
  64. }
  65. $settings = variable_get('custom_formatters_settings', array('contextual' => 1));
  66. $contextual = isset($settings['contextual']) ? $settings['contextual'] : 1;
  67. $list = array_unique(explode("\r\n", isset($settings['contextual_list']) ? $settings['contextual_list'] : ''));
  68. switch ($contextual) {
  69. case 0:
  70. return FALSE;
  71. case 1:
  72. return !in_array($name, $list) ? TRUE : FALSE;
  73. case 2:
  74. return in_array($name, $list) ? TRUE : FALSE;
  75. }
  76. }
  77. /**
  78. * Implements hook_menu_contextual_links_alter().
  79. */
  80. function custom_formatters_menu_contextual_links_alter(&$links, $router_item, $root_path) {
  81. if ($root_path == 'admin/structure/formatters/list/%/edit') {
  82. $links['custom_formatters-edit'] = array_merge(
  83. $router_item,
  84. array(
  85. 'title' => 'Edit formatter',
  86. )
  87. );
  88. }
  89. }