Skip to content

Commit

Permalink
Merge pull request #128 from toofff/feature/add-enabled-attribute-in-…
Browse files Browse the repository at this point in the history
…the-config-ui-elements

Feat: add enabled attribute for the configuration of UiElements
  • Loading branch information
jacquesbh authored Aug 30, 2021
2 parents 9a778eb + debe848 commit 2c65375
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 3 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ monsieurbiz_sylius_richeditor:
tags: ['product']
```

### Deactivate an available element

Here is what really happens when deactivating an Ui Element:
- it's not displayed anymore in frontend
- it's still editable in backend for old contents but you can't add a new one
- if the element has an alias, the alias is treated the same way

Define the overload of a proposed UiElement in your configuration folder, let's say in `config/packages/monsieurbiz_sylius_richeditor_plugin.yaml` as example.

```yaml
monsieurbiz_sylius_richeditor:
ui_elements:
monsieurbiz.youtube:
enabled: false
```

## Available elements

The plugin already contains some simple elements.
Expand Down
8 changes: 6 additions & 2 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ global.MonsieurBizRichEditorManager = class {
elementWrapper.innerHTML = Mustache.render(this.config.elementHtml, {
'title': element.title,
'icon': element.icon,
'preview': element.previewHtml
'preview': element.previewHtml,
'disabled': !element.enabled
});
let uiElement = elementWrapper.firstElementChild;
uiElement.element = element;
Expand Down Expand Up @@ -340,7 +341,10 @@ global.MonsieurBizRichEditorManager = class {
let cardsContainer = this.selectionPanel.dialog.querySelector('.js-uie-cards-container');
cardsContainer.innerHTML = '';
for (let elementCode in this.config.uielements) {
if (this.config.uielements[elementCode].ignored) { // duplicates using aliases
if (
this.config.uielements[elementCode].ignored // duplicates using aliases
|| !this.config.uielements[elementCode].enabled // avoid disabled elements to show up!
) {
continue;
}
if (this.tags.length > 0) {
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private function addUiElements(ArrayNodeDefinition $rootNode): void
->scalarNode('description')->isRequired()->cannotBeEmpty()->end()
->scalarNode('alias')->end()
->scalarNode('icon')->isRequired()->cannotBeEmpty()->end()
->booleanNode('enabled')->defaultTrue()->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/public/js/rich-editor-js.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,5 @@ monsieurbiz_richeditor_plugin:
cannot_parse_json: 'Cannot parse the current content'
cannot_find_type: 'Cannot find type for some UI Elements'
cannot_upload_image: 'Cannot upload the image. The file may be too large or have incorrect format.'
ui:
disabled: 'Disabled'
2 changes: 2 additions & 0 deletions src/Resources/translations/messages.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,5 @@ monsieurbiz_richeditor_plugin:
cannot_parse_json: 'Impossible d''analyser le contenu actuel.'
cannot_find_type: 'Impossible de trouver le type pour certains éléments'
cannot_upload_file: 'Impossible de charger le fichier. Il est possible qu''il soit trop lourd ou son format incorrect.'
ui:
disabled: 'Désactivé'
1 change: 1 addition & 0 deletions src/Resources/views/Admin/app.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<h3 class="header uie-m-0">
<i class="{{icon}} icon"></i>
{{title}}
{{#disabled}}<span class="ui label">{% endverbatim %}{{ 'monsieurbiz_richeditor_plugin.ui.disabled'|trans }}{% verbatim %}</span>{{/disabled}}
</h3>
<div class="ui small basic icon buttons sm:uie-mt-xs">
<button class="ui button js-uie-delete" type="button">
Expand Down
5 changes: 5 additions & 0 deletions src/Twig/RichEditorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ public function renderElement(array $context, array $element): string
}

$uiElement = $this->uiElementRegistry->getUiElement($element['code']);

if (!$uiElement->isEnabled()) {
return '';
}

$template = $uiElement->getFrontRenderTemplate();

$context = array_merge($context, [
Expand Down
8 changes: 8 additions & 0 deletions src/UiElement/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public function getCamelCasedCode(): string
}, $this->getCode());
}

/**
* {@inheritdoc}
*/
public function isEnabled(): bool
{
return $this->parameters['enabled'];
}

/**
* {@inheritdoc}
*/
Expand Down
5 changes: 5 additions & 0 deletions src/UiElement/MetadataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function getAlias(): ?string;
*/
public function getCamelCasedCode(): string;

/**
* @return bool
*/
public function isEnabled(): bool;

/**
* @param string $name
*
Expand Down
5 changes: 5 additions & 0 deletions src/UiElement/UiElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public function getDescription(): string;
*/
public function getIcon(): string;

/**
* @return bool
*/
public function isEnabled(): bool;

/**
* @throws InvalidArgumentException
*/
Expand Down
9 changes: 9 additions & 0 deletions src/UiElement/UiElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public function getAlias(): ?string
return $this->metadata->getAlias();
}

/**
* {@inheritdoc}
*/
public function isEnabled(): bool
{
return $this->metadata->isEnabled();
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -137,6 +145,7 @@ public function jsonSerialize()
'title' => $this->translator->trans($this->getTitle()),
'ignored' => $this->ignored,
'tags' => $this->metadata->getTags(),
'enabled' => $this->isEnabled(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ monsieurbiz_sylius_richeditor:
templates:
admin_render: '/Admin/UiElement/google_maps.html.twig'
front_render: '/Shop/UiElement/google_maps.html.twig'
# monsieurbiz.html:
# enabled: false

0 comments on commit 2c65375

Please sign in to comment.