-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #823 from droptica/feature/generators
Paragraph generators
- Loading branch information
Showing
13 changed files
with
378 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
modules/custom/d_p/src/Generators/ParagraphModuleGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
modules/custom/d_p/src/Generators/SettingClassPluginGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
modules/custom/d_p/src/Generators/SettingPluginGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
modules/custom/d_p/src/Generators/paragraph-preprocess.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
modules/custom/d_p/src/Generators/setting-class-plugin.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ''; | ||
} | ||
} |
Oops, something went wrong.