Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. /**
  2. * Implements hook_theme()
  3. *
  4. * Register a theme for each paragraph type
  5. *
  6. */
  7. function MYMODULE_theme($existing, $type, $theme, $path){
  8. $theme_templates = [];
  9. $paragraph_types = MYMODULE_get_paragraph_types();
  10.  
  11. // Register custom Paragraph bundle templates
  12. foreach ($paragraph_types as $paragraph_type){
  13. $theme_templates['paragraph__MYMODULE__' . $paragraph_type] = [
  14. 'base hook' => 'paragraph'
  15. ];
  16. }
  17.  
  18. // Register custom Paragraph bundle template fallback
  19. $theme_templates['paragraph__MYMODULE'] = [
  20. 'base hook' => 'paragraph'
  21. ];
  22.  
  23. // Register custom Paragraph field wrapper
  24. // $module_path = drupal_get_path('module', 'MYMODULE');
  25. // $theme_templates['paragraph__MYMODULE__field_wrapper'] = [
  26. // 'base hook' => 'field',
  27. // 'template' => 'paragraph--MYMODULE--field-wrapper',
  28. // 'path' => $module_path . '/templates'
  29. // ];
  30.  
  31. return $theme_templates;
  32. }
  33.  
  34. /**
  35. * Implements HOOK_theme_suggestions_HOOK_alter
  36. */
  37. function MYMODULE_theme_suggestions_field_alter(array &$suggestions, array $variables, $hook) {
  38. $field = $variables['element']['#field_name'];
  39.  
  40. if( $field == 'field_content_modules' ) {
  41. $suggestions[] = 'paragraph__MYMODULE__field_wrapper';
  42. }
  43. }
  44.  
  45.  
  46. /**
  47. * Implements HOOK_theme_suggestions_HOOK_alter
  48. */
  49. function MYMODULE_theme_suggestions_paragraph_alter(&$suggestions, $variables){
  50. $entity = $variables['elements']['#paragraph'];
  51. $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
  52. $type = $entity->getType();
  53.  
  54. $suggestions[] = 'paragraph__MYMODULE';
  55. $suggestions[] = 'paragraph__MYMODULE__' . $type;
  56. $suggestions[] = 'paragraph__MYMODULE__' . $type . '__' . $sanitized_view_mode;
  57. }
  58.  
  59.  
  60. /**
  61. * Implements hook_theme_registry_alter()
  62. */
  63. function MYMODULE_theme_registry_alter(&$theme_registry) {
  64. $module_path = drupal_get_path('module', 'MYMODULE');
  65. $template_objects = drupal_find_theme_templates($theme_registry, '.html.twig', $module_path);
  66. $paragraph_types = MYMODULE_get_paragraph_types();
  67.  
  68. // 1. Loop through the paragraph types
  69. // 2. Check if each paragraph exists in the `$template_objects` array
  70. // 3. If it doesn't exist, remove it from the registry so we don't get any errors
  71. // 4. If it does exist, set actual path to template
  72. foreach ($paragraph_types as $type){
  73. $template = 'paragraph__MYMODULE__' . $type;
  74. if(!isset($template_objects[$template])){
  75. unset($theme_registry['paragraph__MYMODULE__' . $type]);
  76. } else {
  77. $theme_registry['paragraph__MYMODULE__' . $type]['path'] = $template_objects[$template]['path'];
  78. }
  79. }
  80. }
  81.  
  82.  
  83. /**
  84. * Helper function to get a list of paragraph types by machine name
  85. *
  86. * @return array
  87. */
  88. function MYMODULE_get_paragraph_types(){
  89. $paragraph_bundles = Drupal::service('entity_type.bundle.info')->getBundleInfo('paragraph');
  90. return array_keys($paragraph_bundles);
  91. }
  92.  
  93. /modules/custom/MYMODULE/templates/paragraph--MYMODULE--field-wrapper.html.twig
  94.  
  95. The website encountered an unexpected error. Please try again later.
  96. Recoverable fatal error: Argument 1 passed to DrupalCoreRenderElement::children() must be of the type array, null given, called in /Users/phil/Sites/glades/www/modules/contrib/paragraphs/paragraphs.module on line 238 and defined in DrupalCoreRenderElement::children() (line 71 of core/lib/Drupal/Core/Render/Element.php).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement