Skip to content

Commit

Permalink
fix event, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Mar 22, 2022
1 parent 5b3688e commit 9e1c919
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 8 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` | `<comma-separated id list of tl_image_size entities> <optional: breakpoints; defaults to 576,768,992,1200,1400>` | 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 [<image-size-ids> [<breakpoints>]]
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
```
135 changes: 135 additions & 0 deletions docs/commands/entity_finder.md
Original file line number Diff line number Diff line change
@@ -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 <table> <id>
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
<?php

namespace App\EventListener;

use Contao\ContentModel;
use Contao\ModuleModel;
use HeimrichHannot\UtilsBundle\Event\ExtendEntityFinderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ExtendEntityFinderSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
ExtendEntityFinderEvent::class => '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;
}
}
}

```
Binary file added docs/commands/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion src/Command/EntityFinderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
4 changes: 2 additions & 2 deletions src/EventListener/InitializeSystemListener.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* Copyright (c) 2021 Heimrich & Hannot GmbH
* Copyright (c) 2022 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/
Expand All @@ -11,7 +11,7 @@
use HeimrichHannot\UtilsBundle\Container\ContainerUtil;

/**
* @Hook("initializeSystem")
* Hook("initializeSystem").
*/
class InitializeSystemListener
{
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ services:
HeimrichHannot\UtilsBundle\EventListener\:
resource: '../../EventListener/*'
autowire: true
autoconfigure: true
public: true
bind:
$bundleConfig: '%huh_utils%'
Expand Down

0 comments on commit 9e1c919

Please sign in to comment.