Skip to content

Commit

Permalink
Merge pull request #92 from jacquesbh/feature/tags
Browse files Browse the repository at this point in the history
Add tags filtering
  • Loading branch information
maximehuran authored Nov 13, 2020
2 parents 0bdc89b + e88388f commit ad82315
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 3 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,33 @@ To display the content of the rich editor field you must call the twig filter:

You can see an example in the [test application](/tests/Application/templates/bundles/SyliusShopBundle/Product/Show/Tabs/Details/_description.html.twig)

### Filter the elements

If you want to filter the elements which are available for your field, you can use the `tags` option when you build your form.
As example:

```php
$builder->add('description', RichEditorType::class, [
'required' => false,
'label' => 'sylius.form.product.description',
'tags' => ['product'],
]);
```

In that example, only the Ui Elements with the tag `product` will be available.
Don't worry, you can add this filter afterwards, we won't remove the already present Ui Elements of your field. But we
won't allow to add more if they don't have one of the allowed tags!

#### Example of setting tags to an Ui Element using yaml

```yaml
monsieurbiz_sylius_richeditor:
ui_elements:
app.my_element:
# …
tags: ['product']
```

## Available elements

The plugin already contains some simple elements.
Expand Down Expand Up @@ -152,6 +179,7 @@ monsieurbiz_sylius_richeditor:
templates:
admin_render: '/Admin/UiElement/google_maps.html.twig'
front_render: '/Shop/UiElement/google_maps.html.twig'
tags: []
```

You can use your own Ui Element object if needed. Be sure to implement the
Expand Down
15 changes: 13 additions & 2 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ global.MonsieurBizRichEditorManager = class {

let inputValue = this.input.value.trim();

this.tags = this.input.dataset.tags.length === 0 ? [] : this.input.dataset.tags.split(',');

let initInterfaceCallback = function () {
this.initInterface();
}.bind(this);
Expand Down Expand Up @@ -325,10 +327,19 @@ 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) {
if (this.config.uielements[elementCode].ignored) { // duplicates using aliases
continue;
}
cardsContainer.append(this.getNewUiElementCard(this.config.uielements[elementCode], position));
if (this.tags.length > 0) {
for (let tagIndex in this.tags) {
if (0 <= this.config.uielements[elementCode].tags.indexOf(this.tags[tagIndex])) {
cardsContainer.append(this.getNewUiElementCard(this.config.uielements[elementCode], position));
break;
}
}
} else {
cardsContainer.append(this.getNewUiElementCard(this.config.uielements[elementCode], position));
}
}
this.newPanel.close();
this.selectionPanel.open();
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ private function addUiElements(ArrayNodeDefinition $rootNode): void
->scalarNode('front_render')->isRequired()->cannotBeEmpty()->end()
->end()
->end()
->arrayNode('tags')
->defaultValue([])
->scalarPrototype()->end()
->end()
->end()
->end()
->end()
Expand Down
13 changes: 13 additions & 0 deletions src/Form/Type/RichEditorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RichEditorType extends TextType
{
Expand All @@ -25,6 +26,18 @@ class RichEditorType extends TextType
public function buildView(FormView $view, FormInterface $form, array $options): void
{
$view->vars['attr']['data-component'] = 'rich-editor';
$view->vars['attr']['data-tags'] = implode(',', $options['tags'] ?? []);
parent::buildView($view, $form, $options);
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'compound' => false,
'tags' => [],
]);
}
}
2 changes: 1 addition & 1 deletion src/Resources/public/js/rich-editor-js.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/UiElement/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,12 @@ public function getServiceId(string $serviceName): string

return sprintf('%s.%s.%s', $code[0], $serviceName, $code[1]);
}

/**
* {@inheritdoc}
*/
public function getTags(): array
{
return $this->parameters['tags'] ?? [];
}
}
5 changes: 5 additions & 0 deletions src/UiElement/MetadataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,9 @@ public function hasTemplate(string $name): bool;
* @return string
*/
public function getServiceId(string $serviceName): string;

/**
* @return array
*/
public function getTags(): array;
}
1 change: 1 addition & 0 deletions src/UiElement/UiElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public function jsonSerialize()
'icon' => $this->getIcon(),
'title' => $this->translator->trans($this->getTitle()),
'ignored' => $this->ignored,
'tags' => $this->metadata->getTags(),
];
}
}
17 changes: 17 additions & 0 deletions tests/spec/UiElement/MetadataSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,21 @@ public function it_gives_its_service_name(): void
{
$this->getServiceId('foo')->shouldReturn('app.foo.amigo');
}

public function it_gives_empty_tags(): void
{
$this->getTags()->shouldReturn([]);
}

public function it_gives_tags(): void
{
$parameters = self::PARAMETERS;
$tags = ['foo', 'bar'];
$parameters['tags'] = $tags;
$this->beConstructedThrough('fromCodeAndConfiguration', [
self::CODE,
$parameters,
]);
$this->getTags()->shouldReturn($tags);
}
}

0 comments on commit ad82315

Please sign in to comment.