Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update modify-grid-in-module.md #1914

Open
wants to merge 1 commit into
base: 8.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 72 additions & 5 deletions development/components/grid/tutorials/modify-grid-in-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,12 @@ $newColumn = new DataColumn('example') ...
$columns->addAfter('name', $newColumn);
```

The operation is **more difficult** with an existing column. We need to remove and add it again to the right position:
With an existing column you can use the:
* `move($columnId, $position)` with the postion being an `int`

```php
<?php
$columns->remove('the_column_we_need_to_move');

$columnWeNeedToMove = new ... // we create the column
$columns->addBefore('sales', $columnWeNeedToMove);
$columns->move('name', 2);
```

### Re-ordering filters?
Expand Down Expand Up @@ -178,3 +176,72 @@ The result column name (here `nb_orders`) must be the same of the `field` option
{{% /notice %}}

Once the new hook is registered and the module activated, you should see the "Customers" grid customized according to the use cases we have listed in the introduction: great!

## Full Example

In tihs example we are adding the APE number to the customer grid and the filter for it.

```php
<?php
use PrestaShop\PrestaShop\Core\Grid\Definition\GridDefinitionInterface;
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\CustomerGridDefinitionFactory;
use PrestaShop\PrestaShop\Core\Grid\Column\Type\DataColumn;
use PrestaShop\PrestaShop\Core\Grid\Filter\Filter;
use Symfony\Component\Form\Extension\Core\Type\TextType;

public function hookActionCustomerGridDefinitionModifier(array $params)
{
$definition = $params['definition'];

// Add the column
$columns = $definition->getColumns();
$columns->addAfter('id_customer',
(new DataColumn('ape'))
->setName('Account #')
->setOptions([
'field' => 'ape'
])
);

# Add the filter
$filters = $definition->getFilters();
$filters->add(
(new Filter('ape', TextType::class))
->setTypeOptions([
'required' => false,
'attr' => [
'placeholder' => 'APE #',
],
])
->setAssociatedColumn('ape')
);
}

public function hookActionCustomerGridQueryBuilderModifier(array $params)
{
// Add ape to the search
$searchQueryBuilder = $params['search_query_builder'];
$searchQueryBuilder->addSelect('c.ape as ape');
$searchCriteria = $params['search_criteria'];

# Adding the filter to the query
foreach ($searchCriteria->getFilters() as $filterName => $filterValue) {
if ('ape' === $filterName) {
$searchQueryBuilder->andWhere('c.ape = :ape');
$searchQueryBuilder->setParameter('ape', $filterValue);
}
}
}
```
{{% notice note %}}
Don't forget to register these hooks in your module
{{% /notice %}}

```php
<?php
public function install() {
return parent::install() &&
$this->registerHook('actionCustomerGridDefinitionModifier') &&
$this->registerHook('actionCustomerGridQueryBuilderModifier');
}
```
Loading