Skip to content

Commit

Permalink
Merge pull request #823 from droptica/feature/generators
Browse files Browse the repository at this point in the history
Paragraph generators
  • Loading branch information
grzegorz-pietrzak-droptica authored Aug 25, 2022
2 parents d462346 + cb78977 commit 06078eb
Show file tree
Hide file tree
Showing 13 changed files with 378 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
8 changes: 8 additions & 0 deletions modules/custom/d_p/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
15 changes: 15 additions & 0 deletions modules/custom/d_p/drush.services.yml
Original file line number Diff line number Diff line change
@@ -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 }
59 changes: 59 additions & 0 deletions modules/custom/d_p/src/Generators/ParagraphModuleGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Drupal\d_p\Generators;

use DrupalCodeGenerator\Command\BaseGenerator;
use DrupalCodeGenerator\Utils;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

/**
* The class that adds Droopler Paragraph generator to Drush.
*/
class ParagraphModuleGenerator extends BaseGenerator {

/**
* {@inheritdoc}
*/
protected $name = 'droopler-paragraph-module';

/**
* {@inheritdoc}
*/
protected $description = 'Generates a Droopler Paragraph module.';

/**
* {@inheritdoc}
*/
protected $alias = 'dropar';

/**
* {@inheritdoc}
*/
protected $templatePath = __DIR__;

/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output) {
$questions = Utils::moduleQuestions();
$questions['preprocess'] = new ConfirmationQuestion('Would you like to create a sample preprocess function to read paragraph settings?', TRUE);

$vars = &$this->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');
}

}
48 changes: 48 additions & 0 deletions modules/custom/d_p/src/Generators/SettingClassPluginGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Drupal\d_p\Generators;

use DrupalCodeGenerator\Command\BaseGenerator;
use DrupalCodeGenerator\Utils;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* The class that adds Droopler Class Setting Plugin generator to Drush.
*/
class SettingClassPluginGenerator extends BaseGenerator {

/**
* {@inheritdoc}
*/
protected $name = 'droopler-setting-class-plugin';

/**
* {@inheritdoc}
*/
protected $description = 'Generates a Droopler Class Setting plugin.';

/**
* {@inheritdoc}
*/
protected $alias = 'drosec';

/**
* {@inheritdoc}
*/
protected $templatePath = __DIR__;

/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output) {
$questions = Utils::moduleQuestions() + Utils::pluginQuestions();

$vars = &$this->collectVars($input, $output, $questions);

$this->addFile()
->path('src/Plugin/ParagraphSetting/{class}.php')
->template('setting-class-plugin.twig');
}

}
47 changes: 47 additions & 0 deletions modules/custom/d_p/src/Generators/SettingPluginGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Drupal\d_p\Generators;

use DrupalCodeGenerator\Command\BaseGenerator;
use DrupalCodeGenerator\Utils;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* The class that adds Droopler Setting Plugin generator to Drush.
*/
class SettingPluginGenerator extends BaseGenerator {

/**
* {@inheritdoc}
*/
protected $name = 'droopler-setting-plugin';

/**
* {@inheritdoc}
*/
protected $description = 'Generates a Droopler Setting plugin.';

/**
* {@inheritdoc}
*/
protected $alias = 'drosep';

/**
* {@inheritdoc}
*/
protected $templatePath = __DIR__;

/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output) {
$questions = Utils::moduleQuestions() + Utils::pluginQuestions();
$vars = &$this->collectVars($input, $output, $questions);

$this->addFile()
->path('src/Plugin/ParagraphSetting/{class}.php')
->template('setting-plugin.twig');
}

}
8 changes: 8 additions & 0 deletions modules/custom/d_p/src/Generators/paragraph-info.twig
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions modules/custom/d_p/src/Generators/paragraph-module.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* @file
* A module with {{ name }} paragraph.
*/
{% if preprocess %}
use Drupal\d_p\Exception\MissingConfigurationStorageFieldException;
use Drupal\d_p\Plugin\Field\FieldType\ConfigurationStorage;
{% endif %}
/**
* Implements hook_theme().
*/
function {{ machine_name }}_theme($existing, $type, $theme, $path): array {
return [
'paragraph__{{ machine_name }}' => [
'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 %}
15 changes: 15 additions & 0 deletions modules/custom/d_p/src/Generators/paragraph-preprocess.twig
Original file line number Diff line number Diff line change
@@ -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());
}
}
60 changes: 60 additions & 0 deletions modules/custom/d_p/src/Generators/paragraph-template.twig
Original file line number Diff line number Diff line change
@@ -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 {{ '%}' }}
<section {{ '{{' }} wrapper_attributes {{ '}}' }}>
<div{{ '{{' }} attributes.addClass(classes) {{ '}}' }}>
<div {{ '{{' }} paragraph_attributes.addClass(paragraph_classes) {{ '}}' }}>
<div class="content-wrapper">
<div{{ '{{' }} content_attributes {{ '}}' }}>
{{ '{%' }} block content {{ '%}' }}
{{ '{{' }} content {{ '}}' }}
{{ '{%' }} endblock {{ '%}' }}
</div>
</div>
</div>
</div>
</section>
{{ '{%' }} endblock paragraph {{ '%}' }}
39 changes: 39 additions & 0 deletions modules/custom/d_p/src/Generators/setting-class-plugin.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
namespace Drupal\{{ machine_name }}\Plugin\ParagraphSetting;
use Drupal\d_p\ParagraphSettingPluginBase;
/**
* Plugin implementation of the '{{ plugin_id }}' class modifier.
*
* @ParagraphSetting(
* id = "{{ plugin_id }}",
* label = @Translation("{{ plugin_label }}"),
* settings = {
* "parent" = "custom_class",
* }
* )
*/
class {{ class }} extends ParagraphSettingPluginBase {
/**
* {@inheritdoc}
*/
public function formElement(): array {
$element = parent::formElement();
return [
'#type' => 'checkbox',
'#description' => $this->t('{{ plugin_label }}'),
] + $element;
}
/**
* {@inheritdoc}
*/
public function getDefaultValue() {
return '';
}
}
36 changes: 36 additions & 0 deletions modules/custom/d_p/src/Generators/setting-plugin.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Drupal\{{ machine_name }}\Plugin\ParagraphSetting;
use Drupal\d_p\ParagraphSettingPluginBase;
/**
* Plugin implementation of the '{{ plugin_id }}' modifier setting.
*
* @ParagraphSetting(
* id = "{{ plugin_id }}",
* label = @Translation("{{ plugin_label }}"),
* )
*/
class {{ class }} extends ParagraphSettingPluginBase {
/**
* {@inheritdoc}
*/
public function formElement(): array {
$element = parent::formElement();
return [
'#type' => 'textfield',
'#description' => $this->t('{{ plugin_label }}'),
] + $element;
}
/**
* {@inheritdoc}
*/
public function getDefaultValue() {
return '';
}
}
Loading

0 comments on commit 06078eb

Please sign in to comment.