Skip to content
Closed
20 changes: 19 additions & 1 deletion paragraphs.module
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* @file
* Paragraphs hooks and common functions.
Expand Down Expand Up @@ -1521,6 +1520,25 @@ function paragraphs_theme() {
);
}

/**
* Implements hook_theme_registry_alter().
*/
function paragraphs_theme_registry_alter(&$theme_registry) {
unset($theme_registry['paragraphs_item']['base hook']);
}

/**
* Implements hook_system_theme_info().
*/
function paragraphs_system_theme_info() {
$path = backdrop_get_path('module', 'paragraphs');
$themes['ptest_theme_1'] = $path . '/tests/ptest_theme_1/ptest_theme_1.info';
$themes['ptest_theme_2'] = $path . '/tests/ptest_theme_2/ptest_theme_2.info';
$themes['ptest_theme_3'] = $path . '/tests/ptest_theme_3/ptest_theme_3.info';
$themes['ptest_theme_4'] = $path . '/tests/ptest_theme_4/ptest_theme_4.info';
return $themes;
}

/**
* Implements hook_field_create_field().
*/
Expand Down
128 changes: 128 additions & 0 deletions tests/paragraphs.test
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
/**
* @file
* Defines tests for paragraphs.
*/

Expand Down Expand Up @@ -214,6 +215,9 @@ class ParagraphsFieldTestCase extends BackdropWebTestCase {
$this->assertText(check_plain($value1), 'First value of paragraph was rendered.');
$this->assertText(check_plain($value2), 'Second value of paragraph was rendered.');

// Check classes.
$class_list = '"paragraphs-item paragraphs-item-ptest paragraphs-item-full paragraphs-item-1"';
$this->assertRaw($class_list, 'Correct class list for paragraphs-item.');
}

/**
Expand Down Expand Up @@ -404,3 +408,127 @@ class ParagraphStatusVisibilityTestCase extends ParagraphsFieldTestCase {
$this->assertNoFieldById('edit-field-paragraphs-und-0-actions-togglepublish-button', 'Publish toggle button not showing.');
}
}

/**
* Test paragraphs item templates.
*/
class ParagraphsThemingTestCase extends ParagraphsFieldTestCase {
const HOST_ENTITY_TYPE = 'paragraph-test';

/**
* Set up.
*/
public function setUp() {
$modules = array(
'paragraphs',
);

parent::setUp($modules);

// Create a content type with a Paragraphs field.
$this->backdropCreateContentType(array(
'type' => 'paragraph_test',
'name' => 'Paragraph Test',
));
$paragraphs_type = new StdClass();
$paragraphs_type->bundle = 'ptest';
$paragraphs_type->name = 'PTest';
$paragraphs_type->label = 'PTest';
$paragraphs_type->description = 'This is the PTest item.';
$paragraphs_type->locked = 1;
$paragraphs_type->allow_unpublish = 0;
paragraphs_bundle_save($paragraphs_type);
$field_settings = array(
'cardinality' => 5,
);
$instance_settings = array(
'title' => 'Paragraph',
'title_multiple' => 'Paragraphs',
'default_edit_mode' => 'preview',
'default_edit_mode_override' => 2,
'add_mode' => 'select',
'modal_admin' => 0,
'allowed_bundles' => array(
'ptest' => 'ptest',
),
'bundle_weights' => array(
'ptest' => '2',
),
'user_register_form' => FALSE,
);
$this->createParagraphsField('field_paragraphs', 'paragraph_test', $field_settings, $instance_settings);
$this->createParagraphsTypeTextField('field_ptest_text', 'ptest', array(), array('required' => 1));

// Create a user with appropriate permissions and log them in.
$this->privilegedUser = $this->backdropCreateUser(array(
'create paragraph_test content',
'edit own paragraph_test content',
'edit any paragraph_test content',
'delete own paragraph_test content',
'delete any paragraph_test content',
));
$this->backdropLogin($this->privilegedUser);
}

/**
* Helper to create a host entity containing a paragraphs item.
*/
private function createNode() {
$this->backdropGet('node/add/' . $this::HOST_ENTITY_TYPE);
$this->backdropPost(NULL, array(), t('Add new Paragraph'));
$title = $this->randomString(20);
$value1 = $this->randomString(20);
$create_edit = array(
'title' => $title,
'field_paragraphs[und][0][field_ptest_text][und][0][value]' => $value1,
);
$this->backdropPost(NULL, $create_edit, t('Save'));
}

/**
* Are we using module-provided paragraph-item.tpl.php?
*/
public function testTemplate() {
theme_enable(array('ptest_theme_1'));
config_set('system.core', 'theme_default', 'ptest_theme_1');
config_set('system.core', 'theme_debug', TRUE);
$this->createNode();
$tpl_identifier = 'paragraphs/theme/paragraphs-item.tpl.php';
$this->assertRaw($tpl_identifier, 'Using paragraphs-item.tpl.php from module.');
}

/**
* Are we using theme-provided paragraphs-item.tpl.php?
*/
public function testOverriddenTemplate() {
theme_enable(array('ptest_theme_2'));
config_set('system.core', 'theme_default', 'ptest_theme_2');
$this->createNode();
$tpl_identifier = '<p>This is the base template.</p>';
$this->assertRaw($tpl_identifier, 'Using paragraphs-item.tpl.php from custom theme.');
}

/**
* Are we using theme-provided paragraphs-item-{bundle}.tpl.php.
*/
public function testBundleTemplate() {
theme_enable(array('ptest_theme_3'));
config_set('system.core', 'theme_default', 'ptest_theme_3');
$this->createNode();
$tpl_identifier = '<p>This is the bundle template.</p>';
$this->assertRaw($tpl_identifier, 'Using paragraphs-item-ptest.tpl.php from custom theme.');
}

/**
* Are we using theme-provided paragraphs-item-{bundle}.tpl.php, when the
* theme also has paragraphs-item.tpl.php?
*/
public function testOverriddenAndBundleTemplate() {
theme_enable(array('ptest_theme_4'));
config_set('system.core', 'theme_default', 'ptest_theme_4');
$this->createNode();
$tpl_identifier = '<p>This is the bundle template.</p>';
$this->assertRaw($tpl_identifier, 'Using paragraphs-item-ptest.tpl.php from custom theme.');
}

}
6 changes: 6 additions & 0 deletions tests/paragraphs.tests.info
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ name = Paragraphs item and bundles status visibility
description = Test status functionality for Paragraphs bundles and items.
group = Paragraphs
file = paragraphs.test

[ParagraphsThemingTestCase]
name = Paragraphs theming
description = Test theme-related functionality for Paragraphs.
group = Paragraphs
file = paragraphs.test
5 changes: 5 additions & 0 deletions tests/ptest_theme_1/ptest_theme_1.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = Paragraphs test theme 1
description = The simplest possible custom theme
type = theme
backdrop = 1.x
hidden = TRUE
32 changes: 32 additions & 0 deletions tests/ptest_theme_2/paragraphs-item.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* @file
* Default theme implementation for a single paragraph item.
*
* Available variables:
* - $admin_links: A dropbutton render array, if separate editing is turned on.
* - $content: An array of content items. Use render($content) to print them
* all, or print a subset such as render($content['field_example']). Use
* hide($content['field_example']) to temporarily suppress the printing of a
* given element.
* - $classes_array: An array of classes that can be used to style contextually through
* CSS. By default the following classes are available, where
* the parts enclosed by {} are replaced by the appropriate values:
* - entity-paragraphs-item
* - paragraphs-item-{bundle}
*
*
* @see template_preprocess()
* @see template_preprocess_entity()
* @see template_process()
*/
?>
<div id="<?php print $variables['html_id']; ?>" class="<?php print implode(' ', $classes_array); ?>"<?php print backdrop_attributes($attributes); ?>>
<?php print render($admin_links); ?>
<?php print render($title_prefix); ?>
<?php print render($title_suffix); ?>
<div class="content"<?php print backdrop_attributes($content_attributes); ?>>
<?php print render($content); ?>
</div>
</div>
<p>This is the base template.</p>
5 changes: 5 additions & 0 deletions tests/ptest_theme_2/ptest_theme_2.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = Paragraphs test theme 2
description = Test overriding paragraphs-item.tpl.php
type = theme
backdrop = 1.x
hidden = TRUE
33 changes: 33 additions & 0 deletions tests/ptest_theme_3/paragraphs-item--ptest.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* @file
* Default theme implementation for a single paragraph item.
*
* Available variables:
* - $admin_links: A dropbutton render array, if separate editing is turned on.
* - $content: An array of content items. Use render($content) to print them
* all, or print a subset such as render($content['field_example']). Use
* hide($content['field_example']) to temporarily suppress the printing of a
* given element.
* - $classes_array: An array of classes that can be used to style contextually through
* CSS. By default the following classes are available, where
* the parts enclosed by {} are replaced by the appropriate values:
* - entity-paragraphs-item
* - paragraphs-item-{bundle}
*
*
* @see template_preprocess()
* @see template_preprocess_entity()
* @see template_process()
*/
?>
<div id="<?php print $variables['html_id']; ?>" class="<?php print implode(' ', $classes_array); ?>"<?php print backdrop_attributes($attributes); ?>>
<?php print render($admin_links); ?>
<?php print render($title_prefix); ?>
<?php print render($title_suffix); ?>
<div class="content"<?php print backdrop_attributes($content_attributes); ?>>
<?php print render($content); ?>
</div>
</div>
<p>This is the bundle template.</p>

5 changes: 5 additions & 0 deletions tests/ptest_theme_3/ptest_theme_3.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = Paragraphs test theme 3
description = Test bundle-specific paragraphs-item template
type = theme
backdrop = 1.x
hidden = TRUE
33 changes: 33 additions & 0 deletions tests/ptest_theme_4/paragraphs-item--ptest.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* @file
* Default theme implementation for a single paragraph item.
*
* Available variables:
* - $admin_links: A dropbutton render array, if separate editing is turned on.
* - $content: An array of content items. Use render($content) to print them
* all, or print a subset such as render($content['field_example']). Use
* hide($content['field_example']) to temporarily suppress the printing of a
* given element.
* - $classes_array: An array of classes that can be used to style contextually through
* CSS. By default the following classes are available, where
* the parts enclosed by {} are replaced by the appropriate values:
* - entity-paragraphs-item
* - paragraphs-item-{bundle}
*
*
* @see template_preprocess()
* @see template_preprocess_entity()
* @see template_process()
*/
?>
<div id="<?php print $variables['html_id']; ?>" class="<?php print implode(' ', $classes_array); ?>"<?php print backdrop_attributes($attributes); ?>>
<?php print render($admin_links); ?>
<?php print render($title_prefix); ?>
<?php print render($title_suffix); ?>
<div class="content"<?php print backdrop_attributes($content_attributes); ?>>
<?php print render($content); ?>
</div>
</div>
<p>This is the bundle template.</p>

32 changes: 32 additions & 0 deletions tests/ptest_theme_4/paragraphs-item.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* @file
* Default theme implementation for a single paragraph item.
*
* Available variables:
* - $admin_links: A dropbutton render array, if separate editing is turned on.
* - $content: An array of content items. Use render($content) to print them
* all, or print a subset such as render($content['field_example']). Use
* hide($content['field_example']) to temporarily suppress the printing of a
* given element.
* - $classes_array: An array of classes that can be used to style contextually through
* CSS. By default the following classes are available, where
* the parts enclosed by {} are replaced by the appropriate values:
* - entity-paragraphs-item
* - paragraphs-item-{bundle}
*
*
* @see template_preprocess()
* @see template_preprocess_entity()
* @see template_process()
*/
?>
<div id="<?php print $variables['html_id']; ?>" class="<?php print implode(' ', $classes_array); ?>"<?php print backdrop_attributes($attributes); ?>>
<?php print render($admin_links); ?>
<?php print render($title_prefix); ?>
<?php print render($title_suffix); ?>
<div class="content"<?php print backdrop_attributes($content_attributes); ?>>
<?php print render($content); ?>
</div>
</div>
<p>This is the base template.</p>
5 changes: 5 additions & 0 deletions tests/ptest_theme_4/ptest_theme_4.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = Paragraphs test theme 4
description = Test bundle-specific template when base template is overridden in the theme
type = theme
backdrop = 1.x
hidden = TRUE
4 changes: 3 additions & 1 deletion theme/paragraphs.theme.inc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ function template_preprocess_paragraphs_item(&$variables, $hook) {

// Gather css classes.
$variables['classes_array'][] = backdrop_html_class($paragraphs_item_type);
$variables['classes_array'][] = backdrop_html_class($paragraphs_item_type . '-' . $bundle);
if ($paragraphs_item_type !== 'paragraphs_item') {
$variables['classes_array'][] = backdrop_html_class($paragraphs_item_type . '-' . $bundle);
}
$variables['classes_array'][] = backdrop_html_class('paragraphs-item-' . $bundle);
$variables['classes_array'][] = backdrop_html_class('paragraphs-item-' . $variables['elements']['#view_mode']);
$variables['classes_array'][] = backdrop_html_class('paragraphs-item-' . $paragraphs_item_id);
Expand Down
Loading