diff --git a/README.md b/README.md index eb1e6efa..01020802 100644 --- a/README.md +++ b/README.md @@ -237,6 +237,21 @@ Use the download extension to render download elements, get download links, down ## Commands -Command | Parameters | Description | Example ---------|------------|-------------|-------- -`huh:utils:create-image-size-items` | ` ` | Creates image size items for a given image size entity. | `huh:utils:create-image-size-items 1,2` +**[Entity finder](docs/commands/entity_finder.md)** - A command to search for any contao entities in your database. + +**Image size creator** - Creates image size items for a given image size entity. + +``` +Description: + Creates image size items for a given image size entity. Image size entities with existing image size items will be skipped. + +Usage: + huh:utils:create-image-size-items [ []] + +Arguments: + image-size-ids The comma separated ids of the image size. Skip the parameter in order to create image size items for all image size entities. + breakpoints The comma separated breakpoints as pixel amounts (defaults to "576,768,992,1200,1400"). [default: "576,768,992,1200,1400"] + +Example: + huh:utils:create-image-size-items 1,2 +``` \ No newline at end of file diff --git a/docs/commands/entity_finder.md b/docs/commands/entity_finder.md new file mode 100644 index 00000000..387dfe17 --- /dev/null +++ b/docs/commands/entity_finder.md @@ -0,0 +1,135 @@ +# Entity finder + +A helper to find where contao entities like frontend module or content elements are located. + +![](screenshot.png) + + +## Features +- search for any entity +- extendable through event + +## Usage + +``` +Description: + A command to find where an entity is included. + +Usage: + huh:utils:entity_finder + +Arguments: + table The database table + id The entity id or alias (id is better supported). +``` + +## Supported tables (out of the box) + +A list about where is searched for parent entities (recursive). + +**`tl_content` (Content elements)** +- parent table + +**`tl_article` (Article):** +- page + +**`tl_block_module` (Block Element child)** +- parent block + +**`tl_block` (Block)** +- block frontend module + +**`tl_module` (Frontend Modules):** +- module content element +- html content element (inserttag insert_module) +- Layouts + +**`tl_layout` (Layout):** +- Themes + +**`tl_theme` (Themes)** + +**`tl_page` (Pages)** + +## Extend + +You can extends the finder command to support additional entities. +Use `ExtendEntityFinderEvent::addParent()` to add parent entites +and `ExtendEntityFinderEvent::setOutput()` for a nice output of the current entity. + +```php + 'onExtendEntityFinderEvent' + ]; + } + + public function onExtendEntityFinderEvent(ExtendEntityFinderEvent $event) + { + switch ($event->getTable()) { + case CustomModel::getTable(): + $entity = CustomModel::findByPk($event->getId()); + + if (!$entity) { + return; + } + $event->addParent(CustomArchiveModel::getTable(), $entity->pid); + + $elements = ContentModel::findBy( + ['type=?','customEntity=?'], + ['custom_entity', $entity->id] + ); + + if ($elements) { + foreach ($elements as $element) { + $event->addParent(ContentModel::getTable(), $element->id); + } + } + + $event->setOutput('Custom entity: '.$entity->title.' (ID: '.$entity->id.')'); + + break; + + case CustomArchiveModel::getTable(): + $archive = CustomArchiveModel::findByPk($event->getId()); + + if ($archive) { + // has no parents, so no addParent() call is needed + $event->setOutput('Custom entity archive: '.$archive->title.' (ID: '.$archive->id.')'); + } + + break; + + // you can also extend other tables to add additional search parameters + case ModuleModel::getTable(): + $model = ModuleModel::findByPk($event->getId()); + + $parents = CustomModel::findBy( + ['includeModule=?'], + [$model->id] + ); + + if ($parents) { + foreach ($parents as $element) { + $event->addParent(CustomModel::getTable(), $element->id); + } + } + + break; + } + } +} + +``` \ No newline at end of file diff --git a/docs/commands/screenshot.png b/docs/commands/screenshot.png new file mode 100644 index 00000000..26b73904 Binary files /dev/null and b/docs/commands/screenshot.png differ diff --git a/src/Command/EntityFinderCommand.php b/src/Command/EntityFinderCommand.php index 2dc9326a..6004d591 100644 --- a/src/Command/EntityFinderCommand.php +++ b/src/Command/EntityFinderCommand.php @@ -70,6 +70,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $result = $this->loop($table, $id); $this->output($io, [$result]); + $io->newLine(); + + return 0; } private function loop(string $table, $id): array @@ -229,7 +232,7 @@ private function runExtendEntityFinderEvent(string $table, $id, array $parents): { /* @var ExtendEntityFinderEvent $event */ if (is_subclass_of($this->eventDispatcher, 'Symfony\Contracts\EventDispatcher\EventDispatcherInterface')) { - $event = $this->eventDispatcher->dispatch(new ExtendEntityFinderEvent($table, $id, $parents)); + $event = $this->eventDispatcher->dispatch(new ExtendEntityFinderEvent($table, $id, $parents), ExtendEntityFinderEvent::class); } else { /** @noinspection PhpParamsInspection */ $event = $this->eventDispatcher->dispatch(ExtendEntityFinderEvent::class, new ExtendEntityFinderEvent($table, $id, $parents)); diff --git a/src/EventListener/ExtendEntityFinderListener.php b/src/EventListener/ExtendEntityFinderSubscriber.php similarity index 83% rename from src/EventListener/ExtendEntityFinderListener.php rename to src/EventListener/ExtendEntityFinderSubscriber.php index 3931c793..3b6979cc 100644 --- a/src/EventListener/ExtendEntityFinderListener.php +++ b/src/EventListener/ExtendEntityFinderSubscriber.php @@ -13,10 +13,18 @@ use HeimrichHannot\Blocks\BlockModel; use HeimrichHannot\Blocks\BlockModuleModel; use HeimrichHannot\UtilsBundle\Event\ExtendEntityFinderEvent; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; -class ExtendEntityFinderListener +class ExtendEntityFinderSubscriber implements EventSubscriberInterface { - public function __invoke(ExtendEntityFinderEvent $event) + public static function getSubscribedEvents() + { + return [ + ExtendEntityFinderEvent::class => 'onExtendEntityFinderEvent' + ]; + } + + public function onExtendEntityFinderEvent(ExtendEntityFinderEvent $event) { if (class_exists(BlockModel::class)) { switch ($event->getTable()) { diff --git a/src/EventListener/InitializeSystemListener.php b/src/EventListener/InitializeSystemListener.php index fc64bc63..190d3e39 100644 --- a/src/EventListener/InitializeSystemListener.php +++ b/src/EventListener/InitializeSystemListener.php @@ -1,7 +1,7 @@