entity_from_query_string.inc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide an relationship handler for entities from query string.
  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 from query string'),
  12. 'keyword' => 'query_string_entity',
  13. 'description' => t('Entity from query string.'),
  14. 'required context' => new ctools_context_required(t('Query string'), 'query_string'),
  15. 'context' => 'ctools_entity_from_query_string_context',
  16. 'edit form' => 'ctools_entity_from_query_string_settings_form',
  17. );
  18. /**
  19. * Return a new context based on an existing context.
  20. */
  21. function ctools_entity_from_query_string_context($context, $conf) {
  22. $entity_type = $conf['entity_type'];
  23. // If unset it wants a generic, unfilled context, which is just NULL.
  24. if (empty($context->data) || !isset($context->data) || !is_numeric($context->data)) {
  25. return ctools_context_create_empty('entity:' . $entity_type, NULL);
  26. }
  27. if (!empty($context->data) && is_numeric($context->data)) {
  28. // Load the entity from the query string value.
  29. $entity_id = $context->data;
  30. $entity = entity_load_single($entity_type, $entity_id);
  31. // Send it to ctools.
  32. return ctools_context_create('entity:' . $entity_type, $entity);
  33. }
  34. }
  35. /**
  36. * Settings form for the relationship.
  37. */
  38. function ctools_entity_from_query_string_settings_form($form, &$form_state) {
  39. //Get all avalible enity types
  40. foreach (entity_get_info() as $key => $value) {
  41. $entity_types[$key] = $value['label'];
  42. }
  43. $form['entity_type'] = array(
  44. '#title' => t('Entity type'),
  45. '#description' => t('Choose entity type to load from query value'),
  46. '#type' => 'select',
  47. '#options' => $entity_types,
  48. );
  49. if (isset($form_state['conf']['entity_type'])) {
  50. $form['entity_type']['#default_value'] = $form_state['conf']['entity_type'];
  51. }
  52. return $form;
  53. }
  54. /**
  55. * Submit handler; settings form for the context.
  56. */
  57. function ctools_entity_from_query_string_settings_form_submit($form, &$form_state) {
  58. $form_state['conf']['entity_type'] = $form_state['values']['entity_type'];
  59. }