entity_bundle.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control based upon entity bundle.
  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("Entity: bundle"),
  12. 'description' => t('Control access by entity bundle.'),
  13. 'callback' => 'ctools_entity_bundle_ctools_access_check',
  14. 'default' => array('type' => array()),
  15. 'settings form' => 'ctools_entity_bundle_ctools_access_settings',
  16. 'settings form submit' => 'ctools_entity_bundle_ctools_access_settings_submit',
  17. 'summary' => 'ctools_entity_bundle_ctools_access_summary',
  18. 'restrictions' => 'ctools_entity_bundle_ctools_access_restrictions',
  19. 'get child' => 'ctools_entity_bundle_ctools_access_get_child',
  20. 'get children' => 'ctools_entity_bundle_ctools_access_get_children',
  21. );
  22. function ctools_entity_bundle_ctools_access_get_child($plugin, $parent, $child) {
  23. $plugins = ctools_entity_bundle_ctools_access_get_children($plugin, $parent);
  24. return $plugins[$parent . ':' . $child];
  25. }
  26. function ctools_entity_bundle_ctools_access_get_children($plugin, $parent) {
  27. $entities = entity_get_info();
  28. $plugins = array();
  29. foreach ($entities as $entity_type => $entity) {
  30. $plugin['title'] = t('@entity: Bundle', array('@entity' => $entity['label']));
  31. $plugin['keyword'] = $entity_type;
  32. $plugin['description'] = t('Control access by @entity entity bundle.', array('@entity' => $entity_type));
  33. $plugin['name'] = $parent . ':' . $entity_type;
  34. $plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type);
  35. $plugins[$parent . ':' . $entity_type] = $plugin;
  36. }
  37. return $plugins;
  38. }
  39. /**
  40. * Settings form for the 'by entity_bundle' access plugin
  41. */
  42. function ctools_entity_bundle_ctools_access_settings($form, &$form_state, $conf) {
  43. $plugin = $form_state['plugin'];
  44. $entity_type = explode(':', $plugin['name']);
  45. $entity_type = $entity_type[1];
  46. $entity = entity_get_info($entity_type);
  47. foreach ($entity['bundles'] as $type => $info) {
  48. $options[$type] = check_plain($info['label']);
  49. }
  50. $form['settings']['type'] = array(
  51. '#title' => t('Entity Bundle'),
  52. '#type' => 'checkboxes',
  53. '#options' => $options,
  54. '#description' => t('Only the checked entity bundles will be valid.'),
  55. '#default_value' => $conf['type'],
  56. );
  57. return $form;
  58. }
  59. /**
  60. * Compress the entity bundles allowed to the minimum.
  61. */
  62. function ctools_entity_bundle_ctools_access_settings_submit($form, &$form_state) {
  63. $form_state['values']['settings']['type'] = array_filter($form_state['values']['settings']['type']);
  64. }
  65. /**
  66. * Check for access.
  67. */
  68. function ctools_entity_bundle_ctools_access_check($conf, $context, $plugin) {
  69. list($plugin_name, $entity_type) = explode(':', $plugin['name']);
  70. if (!$entity_type) {
  71. return FALSE;
  72. };
  73. $entity = entity_get_info($entity_type);
  74. // As far as I know there should always be a context at this point, but this
  75. // is safe.
  76. if (empty($context) || empty($context->data) || empty($context->data->{$entity['entity keys']['bundle']})) {
  77. return FALSE;
  78. }
  79. if (array_filter($conf['type']) && empty($conf['type'][$context->data->{$entity['entity keys']['bundle']}])) {
  80. return FALSE;
  81. }
  82. return TRUE;
  83. }
  84. /**
  85. * Inform the UI that we've eliminated a bunch of possibilities for this
  86. * context.
  87. */
  88. function ctools_entity_bundle_ctools_access_restrictions($conf, &$context) {
  89. if (isset($context->restrictions['type'])) {
  90. $context->restrictions['type'] = array_unique(array_merge($context->restrictions['type'], array_keys(array_filter($conf['type']))));
  91. }
  92. else {
  93. $context->restrictions['type'] = array_keys(array_filter($conf['type']));
  94. }
  95. }
  96. /**
  97. * Provide a summary description based upon the checked entity_bundle.
  98. */
  99. function ctools_entity_bundle_ctools_access_summary($conf, $context, $plugin) {
  100. if (!isset($conf['type'])) {
  101. $conf['type'] = array();
  102. }
  103. list($plugin_name, $entity_type) = explode(':', $plugin['name']);
  104. if (!$entity_type) {
  105. return t('Error, misconfigured entity_bundle access plugin');
  106. };
  107. $entity = entity_get_info($entity_type);
  108. $names = array();
  109. foreach (array_filter($conf['type']) as $type) {
  110. $names[] = check_plain($entity['bundles'][$type]['label']);
  111. }
  112. if (empty($names)) {
  113. return t('@identifier is any bundle', array('@identifier' => $context->identifier));
  114. }
  115. return format_plural(count($names), '@identifier is bundle "@types"', '@identifier bundle is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier));
  116. }