entity.inc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * @file
  4. * Linkit Entity Search Plugin.
  5. */
  6. /**
  7. * Plugin structure is an associative array containing:
  8. * - ui_title: The title of the plugin used in the admin UI.
  9. * - ui_description: A description used in the admin UI.
  10. * - entity_type: (Only required if the plugin extends LinkitPluginEntity) The
  11. * entity type this plugin will be used for.
  12. * - handler: Describes the class to be used for this plugin.
  13. */
  14. $plugin = array(
  15. 'get child' => 'linkit_entity_ctools_linkit_get_child',
  16. 'get children' => 'linkit_entity_ctools_linkit_get_children',
  17. 'handler' => array(
  18. 'class' => 'LinkitSearchPluginEntity',
  19. 'file' => 'entity.class.php',
  20. ),
  21. );
  22. /**
  23. * Get a single Linkit search plugin.
  24. */
  25. function linkit_entity_ctools_linkit_get_child($plugin, $parent, $child) {
  26. $plugins = linkit_entity_ctools_linkit_get_children($plugin, $parent);
  27. return $plugins[$parent . ':' . $child];
  28. }
  29. /**
  30. * Get all child Linkit search plugins.
  31. */
  32. function linkit_entity_ctools_linkit_get_children($plugin, $parent) {
  33. $entities = entity_get_info();
  34. $plugins = array();
  35. // The alternative plugins is extensions of the provided
  36. // LinkitSearchPluginEntity class provided by Linkit.
  37. $alternative_plugins = array(
  38. 'entity:node',
  39. 'entity:user',
  40. 'entity:taxonomy_term',
  41. 'entity:file',
  42. );
  43. foreach ($entities as $entity_type => $entity) {
  44. // The entity must have an URI CALLBACK defined.
  45. if (!isset($entity['uri callback'])) {
  46. continue;
  47. }
  48. $plugin_child = $plugin;
  49. $plugin_child['ui_title'] = $entity['label'];
  50. $plugin_child['ui_description'] = t('Extend Linkit with @entity support.', array('@entity' => $entity_type));
  51. $plugin_child['name'] = $parent . ':' . $entity_type;
  52. $plugin_child['entity_type'] = $entity_type;
  53. // If there is an alternative search class for this entity, change the handler.
  54. if (in_array('entity:' . $entity_type, $alternative_plugins)) {
  55. $handler = array(
  56. 'class' => 'LinkitSearchPlugin' . drupal_ucfirst($entity_type),
  57. 'file' => $entity_type . '.class.php',
  58. );
  59. $plugin_child['handler'] = $handler;
  60. }
  61. drupal_alter('linkit_search_plugin_entity', $plugin_child, $entity);
  62. $plugins[$parent . ':' . $entity_type] = $plugin_child;
  63. }
  64. drupal_alter('linkit_search_plugin_entities', $plugins);
  65. // Test the plugins.
  66. foreach ($plugins as $plugin_name => $plugin) {
  67. // In some cases the plugins isn't auto loaded. Ctools handle this for us.
  68. // This is typical when installing Linkit.
  69. if (!class_exists($plugin['handler']['class'])) {
  70. continue;
  71. }
  72. $tested_plugin = new $plugin['handler']['class']($plugin, new LinkitProfile());
  73. if (isset($tested_plugin->unusable) && $tested_plugin->unusable) {
  74. unset($plugins[$plugin_name]);
  75. }
  76. }
  77. return $plugins;
  78. }