Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implements hook_theme()
- *
- * Register a theme for each paragraph type
- *
- */
- function MYMODULE_theme($existing, $type, $theme, $path){
- $theme_templates = [];
- $paragraph_types = MYMODULE_get_paragraph_types();
- // Register custom Paragraph bundle templates
- foreach ($paragraph_types as $paragraph_type){
- $theme_templates['paragraph__MYMODULE__' . $paragraph_type] = [
- 'base hook' => 'paragraph'
- ];
- }
- // Register custom Paragraph bundle template fallback
- $theme_templates['paragraph__MYMODULE'] = [
- 'base hook' => 'paragraph'
- ];
- // Register custom Paragraph field wrapper
- // $module_path = drupal_get_path('module', 'MYMODULE');
- // $theme_templates['paragraph__MYMODULE__field_wrapper'] = [
- // 'base hook' => 'field',
- // 'template' => 'paragraph--MYMODULE--field-wrapper',
- // 'path' => $module_path . '/templates'
- // ];
- return $theme_templates;
- }
- /**
- * Implements HOOK_theme_suggestions_HOOK_alter
- */
- function MYMODULE_theme_suggestions_field_alter(array &$suggestions, array $variables, $hook) {
- $field = $variables['element']['#field_name'];
- if( $field == 'field_content_modules' ) {
- $suggestions[] = 'paragraph__MYMODULE__field_wrapper';
- }
- }
- /**
- * Implements HOOK_theme_suggestions_HOOK_alter
- */
- function MYMODULE_theme_suggestions_paragraph_alter(&$suggestions, $variables){
- $entity = $variables['elements']['#paragraph'];
- $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
- $type = $entity->getType();
- $suggestions[] = 'paragraph__MYMODULE';
- $suggestions[] = 'paragraph__MYMODULE__' . $type;
- $suggestions[] = 'paragraph__MYMODULE__' . $type . '__' . $sanitized_view_mode;
- }
- /**
- * Implements hook_theme_registry_alter()
- */
- function MYMODULE_theme_registry_alter(&$theme_registry) {
- $module_path = drupal_get_path('module', 'MYMODULE');
- $template_objects = drupal_find_theme_templates($theme_registry, '.html.twig', $module_path);
- $paragraph_types = MYMODULE_get_paragraph_types();
- // 1. Loop through the paragraph types
- // 2. Check if each paragraph exists in the `$template_objects` array
- // 3. If it doesn't exist, remove it from the registry so we don't get any errors
- // 4. If it does exist, set actual path to template
- foreach ($paragraph_types as $type){
- $template = 'paragraph__MYMODULE__' . $type;
- if(!isset($template_objects[$template])){
- unset($theme_registry['paragraph__MYMODULE__' . $type]);
- } else {
- $theme_registry['paragraph__MYMODULE__' . $type]['path'] = $template_objects[$template]['path'];
- }
- }
- }
- /**
- * Helper function to get a list of paragraph types by machine name
- *
- * @return array
- */
- function MYMODULE_get_paragraph_types(){
- $paragraph_bundles = Drupal::service('entity_type.bundle.info')->getBundleInfo('paragraph');
- return array_keys($paragraph_bundles);
- }
- /modules/custom/MYMODULE/templates/paragraph--MYMODULE--field-wrapper.html.twig
- The website encountered an unexpected error. Please try again later.
- 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