translation_exists.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control on entities based on translations.
  5. */
  6. /**
  7. * CTools Plugin definition.
  8. */
  9. $plugin = array(
  10. 'title' => t("Entity translation: translation exists"),
  11. 'description' => t('Control access by checking if a translation exists.'),
  12. 'callback' => 'entity_translation_translation_exists_ctools_access_check',
  13. 'default' => array('language' => array()),
  14. 'settings form' => 'entity_translation_translation_exists_ctools_access_settings',
  15. 'settings form submit' => 'entity_translation_translation_exists_ctools_access_settings_submit',
  16. 'summary' => 'entity_translation_translation_exists_ctools_access_summary',
  17. 'get child' => 'entity_translation_translation_exists_ctools_access_get_child',
  18. 'get children' => 'entity_translation_translation_exists_ctools_access_get_children',
  19. );
  20. /**
  21. * Get a particular instance of this plugin.
  22. */
  23. function entity_translation_translation_exists_ctools_access_get_child($plugin, $parent, $child) {
  24. $plugins = &drupal_static(__FUNCTION__, array());
  25. if (empty($plugins[$parent . ':' . $child])) {
  26. $plugins[$parent . ':' . $child] = _entity_translation_translation_exists_ctools_access_definition($plugin, $parent, $child);
  27. }
  28. return $plugins[$parent . ':' . $child];
  29. }
  30. /**
  31. * Get all children of this plugin.
  32. */
  33. function entity_translation_translation_exists_ctools_access_get_children($plugin, $parent) {
  34. $plugins = &drupal_static(__FUNCTION__, array());
  35. if (!empty($plugins)) {
  36. return $plugins;
  37. }
  38. $entities = entity_get_info();
  39. foreach ($entities as $entity_type => $entity) {
  40. $plugin = _entity_translation_translation_exists_ctools_access_definition($plugin, $parent, $entity_type);
  41. $plugins[$parent . ':' . $entity_type] = $plugin;
  42. }
  43. return $plugins;
  44. }
  45. /**
  46. * Plugin definition for one particular plugin child.
  47. */
  48. function _entity_translation_translation_exists_ctools_access_definition($plugin, $parent, $entity_type) {
  49. $entity = entity_get_info($entity_type);
  50. $plugin['title'] = t('@entity: entity translation exists', array('@entity' => $entity['label']));
  51. $plugin['keyword'] = $entity_type;
  52. $plugin['description'] = t('Control access by @entity language', array('@entity' => $entity['label']));
  53. $plugin['name'] = $parent . ':' . $entity_type;
  54. $plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type);
  55. return $plugin;
  56. }
  57. /**
  58. * Settings form for the 'by node_language' access plugin
  59. */
  60. function entity_translation_translation_exists_ctools_access_settings($form, &$form_state, $conf) {
  61. $options = array(
  62. ENTITY_TRANSLATION_LANGUAGE_CURRENT => t('Current content language'),
  63. ENTITY_TRANSLATION_LANGUAGE_DEFAULT => t('Default site language'),
  64. LANGUAGE_NONE => t('Language neutral'),
  65. );
  66. $options = array_merge($options, locale_language_list());
  67. $form['settings']['language'] = array(
  68. '#title' => t('Language'),
  69. '#type' => 'checkboxes',
  70. '#options' => $options,
  71. '#description' => t('This rule will pass if any of these languages are present.'),
  72. '#default_value' => $conf['language'],
  73. );
  74. return $form;
  75. }
  76. /**
  77. * Grant access based on the which translations are available.
  78. */
  79. function entity_translation_translation_exists_ctools_access_check($conf, $context) {
  80. // Check that the context exists.
  81. if (empty($context) || empty($context->data)) {
  82. return FALSE;
  83. }
  84. $entity = $context->data;
  85. $handler = entity_translation_get_handler($context->keyword, $entity);
  86. if (!empty($handler)) {
  87. $translations = $handler->getTranslations();
  88. global $language_content;
  89. foreach ($conf['language'] as $lang) {
  90. if ($lang) {
  91. switch ($lang) {
  92. case ENTITY_TRANSLATION_LANGUAGE_CURRENT:
  93. $lang = $language_content->language;
  94. break;
  95. case ENTITY_TRANSLATION_LANGUAGE_DEFAULT:
  96. $lang = language_default('language');
  97. break;
  98. }
  99. if (isset($translations->data[$lang]) && $translations->data[$lang]['status']) {
  100. return TRUE;
  101. }
  102. }
  103. }
  104. }
  105. return FALSE;
  106. }
  107. /**
  108. * Provide a summary description based upon the checked node_languages.
  109. */
  110. function entity_translation_translation_exists_ctools_access_summary($conf, $context) {
  111. $languages = array(
  112. ENTITY_TRANSLATION_LANGUAGE_CURRENT => t('Current site content language'),
  113. ENTITY_TRANSLATION_LANGUAGE_DEFAULT => t('Default site language'),
  114. LANGUAGE_NONE => t('Language neutral'),
  115. );
  116. $languages = array_merge($languages, locale_language_list());
  117. if (!isset($conf['language'])) {
  118. $conf['language'] = array();
  119. }
  120. $names = array();
  121. foreach (array_filter($conf['language']) as $language) {
  122. $names[] = $languages[$language];
  123. }
  124. if (empty($names)) {
  125. return t('@identifier is in any language', array('@identifier' => $context->identifier));
  126. }
  127. return format_plural(count($names), '@languages translation exists for @identifier', '@languages translations exists for identifier', array('@languages' => implode(', ', $names), '@identifier' => $context->identifier));
  128. }