PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/Field/src/Event/FieldHook.php

http://github.com/QuickAppsCMS/QuickApps-CMS
PHP | 111 lines | 35 code | 8 blank | 68 comment | 2 complexity | ca069c436634603406100f2e5c284f96 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  1. <?php
  2. /**
  3. * Licensed under The GPL-3.0 License
  4. * For full copyright and license information, please see the LICENSE.txt
  5. * Redistributions of files must retain the above copyright notice.
  6. *
  7. * @since 2.0.0
  8. * @author Christopher Castro <chris@quickapps.es>
  9. * @link http://www.quickappscms.org
  10. * @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License
  11. */
  12. namespace Field\Event;
  13. use Cake\Event\Event;
  14. use Cake\Event\EventListenerInterface;
  15. use CMS\Event\EventDispatcherTrait;
  16. use CMS\View\ViewModeAwareTrait;
  17. /**
  18. * Field rendering dispatcher.
  19. *
  20. * Dispatches `Render.Field\Model\Entity\Field` rendering-request from View to
  21. * their corresponding FieldHandlers.
  22. *
  23. * Field Handlers should implement the `<FieldHandler>.Entity.display` hook. e.g.:
  24. *
  25. * ```
  26. * Field.TextField.Entity.display
  27. * ```
  28. *
  29. * Its callable method should expect two parameters, `$field` and `$options`, and it
  30. * should return a HTML string representation of $field. i.e.:
  31. *
  32. * ```php
  33. * public function display(Event $event, $field, $options) {
  34. * return
  35. * "<h2>{$field->label}</h2>" .
  36. * "<p>{$field->value}</p>";
  37. * }
  38. * ```
  39. *
  40. * Usually you will rely on view elements for HTML rendering, to invoke
  41. * View::element(...), you should use event's subject which is the view instance
  42. * being used:
  43. *
  44. * ```php
  45. * public function display(Event $event, $field, $options) {
  46. * return $event
  47. * ->subject()
  48. * ->element('MyPlugin.text_field_display', compact('field', 'options'));
  49. * }
  50. * ```
  51. *
  52. * Remember that view elements can alway be overwritten by themes. So it's a good
  53. * practice always use view elements as rendering method instead returning
  54. * hard-coded HTML code.
  55. */
  56. class FieldHook implements EventListenerInterface
  57. {
  58. use EventDispatcherTrait;
  59. use ViewModeAwareTrait;
  60. /**
  61. * Returns a list of hooks this Hook Listener is implementing. When the class is
  62. * registered in an event manager, each individual method will be associated with
  63. * the respective event.
  64. *
  65. * @return void
  66. */
  67. public function implementedEvents()
  68. {
  69. return [
  70. 'Render.Field\Model\Entity\Field' => [
  71. 'callable' => 'renderField',
  72. 'priority' => -1
  73. ],
  74. ];
  75. }
  76. /**
  77. * We catch all field rendering request (from CMS\View\View) here, then we
  78. * dispatch to their corresponding FieldHandler.
  79. *
  80. * If the field object being rendered has been set to "hidden" for the current
  81. * view mode it will not be rendered.
  82. *
  83. * @param \Cake\Event\Event $event The event that was triggered
  84. * @param \Field\Model\Entity\Field $field Mock entity
  85. * @param array $options Additional array of options
  86. * @return string The rendered field
  87. */
  88. public function renderField(Event $event, $field, $options = [])
  89. {
  90. $viewMode = $this->viewMode();
  91. if (isset($field->metadata->view_modes[$viewMode]) &&
  92. !$field->metadata->view_modes[$viewMode]['hidden']
  93. ) {
  94. $event->stopPropagation(); // We don't want other plugins to catch this
  95. $result = (string)$field->render($event->subject());
  96. if (!$field->metadata->view_modes[$viewMode]['shortcodes']) {
  97. $result = $event->subject()->stripShortcodes($result);
  98. }
  99. return $result;
  100. }
  101. return '';
  102. }
  103. }