flag_link.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Plugin definition.
  4. */
  5. $plugin = array(
  6. 'title' => t('Flag link'),
  7. 'description' => t('Add a flag link of a certain flag type for entities.'),
  8. 'defaults' => array('flag_name' => ''),
  9. 'content type' => 'flag_flag_link_content_type_info',
  10. );
  11. /**
  12. * Get the entity content type info.
  13. */
  14. function flag_flag_link_content_type_info($entity_type) {
  15. $types = flag_flag_link_content_type_content_types();
  16. if (isset($types[$entity_type])) {
  17. return $types[$entity_type];
  18. }
  19. }
  20. /**
  21. * Implements hook_PLUGIN_content_type_content_types().
  22. */
  23. function flag_flag_link_content_type_content_types() {
  24. $types = &drupal_static(__FUNCTION__);
  25. if (isset($types)) {
  26. return $types;
  27. }
  28. $types = array();
  29. $entities = entity_get_info();
  30. foreach ($entities as $entity_type => $info) {
  31. $types[$entity_type] = array(
  32. 'title' => t('Flag for @entity_type', array('@entity_type' => $info['label'])),
  33. 'category' => t('Entity'),
  34. 'required context' => new ctools_context_required(t('Entity'), $entity_type),
  35. );
  36. }
  37. return $types;
  38. }
  39. /**
  40. * Render callback.
  41. */
  42. function flag_flag_link_content_type_render($subtype, $conf, $args, $context) {
  43. $flag = flag_get_flag($conf['flag_name']);
  44. if (!$flag) {
  45. return;
  46. }
  47. if (empty($context->data)) {
  48. return;
  49. }
  50. // Get the ID of the entity.
  51. list($id,,) = entity_extract_ids($flag->entity_type, $context->data);
  52. $link = flag_create_link($flag->name, $id);
  53. if (!$link) {
  54. return;
  55. }
  56. $block = new stdClass();
  57. $block->module = 'flag';
  58. $block->title = t('Flag link');
  59. $block->delta = $flag->name;
  60. $block->content = $link;
  61. return $block;
  62. }
  63. /**
  64. * Form callback.
  65. */
  66. function flag_flag_link_content_type_edit_form($form, &$form_state) {
  67. $conf = $form_state['conf'];
  68. $entity_type = $form_state['subtype_name'];
  69. $options = array();
  70. foreach (flag_get_flags($entity_type) as $flag) {
  71. $options[$flag->name] = $flag->title;
  72. }
  73. $form['flag_name'] = array(
  74. '#type' => 'select',
  75. '#title' => t('Flag name'),
  76. '#default_value' => $conf['flag_name'],
  77. '#options' => $options,
  78. '#description' => t('Select a flag.'),
  79. '#required' => TRUE,
  80. '#disabled' => !$options,
  81. );
  82. return $form;
  83. }
  84. /**
  85. * Form submit.
  86. */
  87. function flag_flag_link_content_type_edit_form_submit($form, &$form_state) {
  88. // Copy everything from our defaults.
  89. foreach (array_keys($form_state['plugin']['defaults']) as $key) {
  90. $form_state['conf'][$key] = $form_state['values'][$key];
  91. }
  92. }
  93. /**
  94. * Returns the administrative title for a flag link.
  95. */
  96. function flag_flag_link_content_type_admin_title($subtype, $conf) {
  97. if ($flag = flag_get_flag($conf['flag_name'])) {
  98. return t('Flag link of @flag', array('@flag' => $flag->title));
  99. }
  100. }