diff --git a/modules/custom/d_commerce/modules/d_commerce_products_list/d_commerce_products_list.module b/modules/custom/d_commerce/modules/d_commerce_products_list/d_commerce_products_list.module index 579a5a39f..e8cd259c3 100644 --- a/modules/custom/d_commerce/modules/d_commerce_products_list/d_commerce_products_list.module +++ b/modules/custom/d_commerce/modules/d_commerce_products_list/d_commerce_products_list.module @@ -30,7 +30,7 @@ function d_commerce_products_list_preprocess_commerce_product(&$variables) { /** * Implements hook_preprocess_THEME(). */ -function d_commerce_product_preprocess_commerce_product__droopler_products__teaser(&$variables) { +function d_commerce_products_list_preprocess_commerce_product__droopler_products__teaser(&$variables) { if (isset($variables['product']['variation_title'])) { $variation = $variables['product']['variation_title']['#object']; $view_builder = \Drupal::entityTypeManager()->getViewBuilder($variation->getEntityTypeId()); diff --git a/modules/custom/d_p/README.md b/modules/custom/d_p/README.md index 05c181fe6..f9b992e0e 100644 --- a/modules/custom/d_p/README.md +++ b/modules/custom/d_p/README.md @@ -83,3 +83,11 @@ it instead. You can also use hooks to alter the widget forms: **Note:** This method is not recommended, as long as there is no other way to alter the form elements or validate them using the alter methods described above. + +### Drush generators ### + +You can use Drush to generate code for paragraphs and settings. +Please remember that the paragraph's machine name **must be the same** as module's machine name! +* `drush generate droopler-paragraph-module` - generates a module with a custom paragraph twig template. Please use your paragraph machine name as the module machine name. +* `drush generate droopler-setting-plugin` - generates a plugin with custom setting plugin. +* `drush generate droopler-setting-class-plugin` - generates a plugin with custom setting plugin (dependent on parent class plugin). diff --git a/modules/custom/d_p/drush.services.yml b/modules/custom/d_p/drush.services.yml new file mode 100644 index 000000000..9fb4cb533 --- /dev/null +++ b/modules/custom/d_p/drush.services.yml @@ -0,0 +1,15 @@ +services: + droopler.paragraph_generator: + class: Drupal\d_p\Generators\ParagraphModuleGenerator + tags: + - { name: drush.generator } + + droopler.setting_plugin_generator: + class: Drupal\d_p\Generators\SettingPluginGenerator + tags: + - { name: drush.generator } + + droopler.setting_class_plugin_generator: + class: Drupal\d_p\Generators\SettingClassPluginGenerator + tags: + - { name: drush.generator } diff --git a/modules/custom/d_p/src/Generators/ParagraphModuleGenerator.php b/modules/custom/d_p/src/Generators/ParagraphModuleGenerator.php new file mode 100644 index 000000000..d7554301e --- /dev/null +++ b/modules/custom/d_p/src/Generators/ParagraphModuleGenerator.php @@ -0,0 +1,59 @@ +collectVars($input, $output, $questions); + $vars['template'] = 'paragraph--' . str_replace('_', '-', $vars['machine_name']); + + $this->addFile() + ->path('{machine_name}.module') + ->template('paragraph-module.twig'); + + $this->addFile() + ->path('{machine_name}.info.yml') + ->template('paragraph-info.twig'); + + $this->addFile() + ->path('templates/{template}.html.twig') + ->template('paragraph-template.twig'); + } + +} diff --git a/modules/custom/d_p/src/Generators/SettingClassPluginGenerator.php b/modules/custom/d_p/src/Generators/SettingClassPluginGenerator.php new file mode 100644 index 000000000..a6c2ddb6b --- /dev/null +++ b/modules/custom/d_p/src/Generators/SettingClassPluginGenerator.php @@ -0,0 +1,48 @@ +collectVars($input, $output, $questions); + + $this->addFile() + ->path('src/Plugin/ParagraphSetting/{class}.php') + ->template('setting-class-plugin.twig'); + } + +} diff --git a/modules/custom/d_p/src/Generators/SettingPluginGenerator.php b/modules/custom/d_p/src/Generators/SettingPluginGenerator.php new file mode 100644 index 000000000..0c4bebd67 --- /dev/null +++ b/modules/custom/d_p/src/Generators/SettingPluginGenerator.php @@ -0,0 +1,47 @@ +collectVars($input, $output, $questions); + + $this->addFile() + ->path('src/Plugin/ParagraphSetting/{class}.php') + ->template('setting-plugin.twig'); + } + +} diff --git a/modules/custom/d_p/src/Generators/paragraph-info.twig b/modules/custom/d_p/src/Generators/paragraph-info.twig new file mode 100644 index 000000000..d4c02f8b3 --- /dev/null +++ b/modules/custom/d_p/src/Generators/paragraph-info.twig @@ -0,0 +1,8 @@ +name: {{ name }} +type: module +description: The {{ machine_name }} paragraph. +package: Paragraphs +core: 8.x +core_version_requirement: ^8 || ^9 +dependencies: + - paragraphs:paragraphs diff --git a/modules/custom/d_p/src/Generators/paragraph-module.twig b/modules/custom/d_p/src/Generators/paragraph-module.twig new file mode 100644 index 000000000..674888e84 --- /dev/null +++ b/modules/custom/d_p/src/Generators/paragraph-module.twig @@ -0,0 +1,40 @@ + [ + 'base hook' => 'paragraph', + ], + ]; +} + +{% if preprocess %} +/** + * Implements hook_preprocess_HOOK(). + */ +function {{ machine_name }}_preprocess_paragraph__{{ machine_name }}(array &$variables) { + $paragraph = $variables['paragraph']; + try { + /** @var \Drupal\d_p\Plugin\Field\ConfigurationStorageFieldItemListInterface $setting_field */ + $setting_field = ConfigurationStorage::getSettingsFieldFromEntity($paragraph); + + $variables['foo'] = $setting_field->getSettingValue('foo'); + } + catch (MissingConfigurationStorageFieldException $exception) { + \Drupal::logger('{{ machine_name }}')->error($exception->getMessage()); + } +} +{% endif %} diff --git a/modules/custom/d_p/src/Generators/paragraph-preprocess.twig b/modules/custom/d_p/src/Generators/paragraph-preprocess.twig new file mode 100644 index 000000000..7e3705c1a --- /dev/null +++ b/modules/custom/d_p/src/Generators/paragraph-preprocess.twig @@ -0,0 +1,15 @@ +/** + * Implements hook_preprocess_HOOK(). + */ +function {{ machine_name }}_preprocess_paragraph__{{ machine_name }}(array &$variables) { + $paragraph = $variables['paragraph']; + try { + /** @var \Drupal\d_p\Plugin\Field\ConfigurationStorageFieldItemListInterface $setting_field */ + $setting_field = ConfigurationStorage::getSettingsFieldFromEntity($paragraph); + + $variables['foo'] = $setting_field->getSettingValue('foo'); + } + catch (MissingConfigurationStorageFieldException $exception) { + \Drupal::logger('{{ machine_name }}')->error($exception->getMessage()); + } +} diff --git a/modules/custom/d_p/src/Generators/paragraph-template.twig b/modules/custom/d_p/src/Generators/paragraph-template.twig new file mode 100644 index 000000000..6dbe7672d --- /dev/null +++ b/modules/custom/d_p/src/Generators/paragraph-template.twig @@ -0,0 +1,60 @@ +{{ '{#' }} +/** + * @file + * Default theme implementation to display a paragraph. + * + * Available variables: + * - paragraph: Full paragraph entity. + * Only method names starting with "get", "has", or "is" and a few common + * methods such as "id", "label", and "bundle" are available. For example: + * - paragraph.getCreatedTime() will return the paragraph creation timestamp. + * - paragraph.id(): The paragraph ID. + * - paragraph.bundle(): The type of the paragraph, for example, "image" or "text". + * - paragraph.getOwnerId(): The user ID of the paragraph author. + * See Drupal\paragraphs\Entity\Paragraph for a full list of public properties + * and methods for the paragraph object. + * - content: All paragraph items. Use {{ '{{' }} content {{ '}}' }} to print them all, + * or print a subset such as {{ '{{' }} content.field_example {{ '}}' }}. Use + * {{ '{{' }} content|without('field_example') {{ '}}' }} to temporarily suppress the printing + * of a given child element. + * - attributes: HTML attributes for the containing element. + * The attributes.class element may contain one or more of the following + * classes: + * - paragraphs: The current template type (also known as a "theming hook"). + * - paragraphs--type-[type]: The current paragraphs type. For example, if the paragraph is an + * "Image" it would result in "paragraphs--type--image". Note that the machine + * name will often be in a short form of the human readable label. + * - paragraphs--view-mode--[view_mode]: The View Mode of the paragraph; for example, a + * preview would result in: "paragraphs--view-mode--preview", and + * default: "paragraphs--view-mode--default". + * - view_mode: View mode; for example, "preview" or "full". + * - logged_in: Flag for authenticated user status. Will be true when the + * current user is a logged-in member. + * - is_admin: Flag for admin user status. Will be true when the current user + * is an administrator. + * + * @see template_preprocess_paragraph() + * + * @ingroup themeable + */ +{{ '#}' }} +{{ '{%' }} set classes = [ + 'paragraph', + 'paragraph--type--' ~ paragraph.bundle|clean_class, + view_mode ? 'paragraph--view-mode--' ~ view_mode|clean_class, +] {{ '%}' }} +{{ '{%' }} block paragraph {{ '%}' }} +
+ +
+
+ + {{ '{%' }} block content {{ '%}' }} + {{ '{{' }} content {{ '}}' }} + {{ '{%' }} endblock {{ '%}' }} +
+
+ + +
+{{ '{%' }} endblock paragraph {{ '%}' }} diff --git a/modules/custom/d_p/src/Generators/setting-class-plugin.twig b/modules/custom/d_p/src/Generators/setting-class-plugin.twig new file mode 100644 index 000000000..9f2e87f7b --- /dev/null +++ b/modules/custom/d_p/src/Generators/setting-class-plugin.twig @@ -0,0 +1,39 @@ + 'checkbox', + '#description' => $this->t('{{ plugin_label }}'), + ] + $element; + } + + /** + * {@inheritdoc} + */ + public function getDefaultValue() { + return ''; + } + +} diff --git a/modules/custom/d_p/src/Generators/setting-plugin.twig b/modules/custom/d_p/src/Generators/setting-plugin.twig new file mode 100644 index 000000000..25ea241ef --- /dev/null +++ b/modules/custom/d_p/src/Generators/setting-plugin.twig @@ -0,0 +1,36 @@ + 'textfield', + '#description' => $this->t('{{ plugin_label }}'), + ] + $element; + } + + /** + * {@inheritdoc} + */ + public function getDefaultValue() { + return ''; + } + +} diff --git a/modules/custom/d_p_text_blocks/d_p_text_blocks.module b/modules/custom/d_p_text_blocks/d_p_text_blocks.module index 75d0c62e9..6aab52c20 100644 --- a/modules/custom/d_p_text_blocks/d_p_text_blocks.module +++ b/modules/custom/d_p_text_blocks/d_p_text_blocks.module @@ -45,7 +45,7 @@ function d_p_text_blocks_preprocess_field(&$variables) { // Enable grid setting. if ($setting_field->hasClass('with-grid')) { - set_columns_sizes($variables['items'], $columns); + d_p_text_blocks_set_columns_sizes($variables['items'], $columns); } else { $grid_size = 12; @@ -74,7 +74,7 @@ function d_p_text_blocks_preprocess_field(&$variables) { * @param array $column_count * Array with the number of columns for the selected breakpoints. */ -function set_columns_sizes(array $items, array $column_count) { +function d_p_text_blocks_set_columns_sizes(array $items, array $column_count) { foreach ($column_count as $breakpoint => $items_per_row) { if (!empty($items_per_row)) { foreach ($items as $idx => $list_item) {