Skip to content

[Admin][TwigHook] Allow configuring primary Twig hook for CRUD templates#17645

Merged
GSadee merged 1 commit intoSylius:2.0from
Wojdylak:twig-hooks-add-hook-prefix
Jan 30, 2025
Merged

[Admin][TwigHook] Allow configuring primary Twig hook for CRUD templates#17645
GSadee merged 1 commit intoSylius:2.0from
Wojdylak:twig-hooks-add-hook-prefix

Conversation

@Wojdylak
Copy link
Copy Markdown
Member

@Wojdylak Wojdylak commented Jan 29, 2025

Q A
Branch? 2.0
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Related tickets n/a
License MIT

By default, when using a resource controller with admin CRUD templates in plugins or applications, the hook prefix is hard coded as sylius_admin.%resource_name%. This PR introduces the ability to configure additional prefix using a new hook_prefix option, allowing for more flexibility in defining hook names.

sylius_refund_credit_memo:
    resource: |
        alias: sylius_refund.credit_memo
        section: admin
        templates: "@SyliusAdmin\\shared\\crud"
        only: ['index']
        grid: sylius_refund_credit_memo
        permission: true
        vars:
            all:
                hook_prefix: sylius_refund.admin.credit_memo

With this configuration. hooks related to this resource will use sylius_refund.admin.credit_memo and sylius_admin.credit_memo

Summary by CodeRabbit

  • Refactor
    • Updated hook prefix handling in multiple Twig templates to support more dynamic configuration
    • Introduced a flexible prefixes variable to manage hook references across different admin templates
    • Enhanced template configuration with conditional prefix assignment

@Wojdylak Wojdylak requested review from a team as code owners January 29, 2025 18:46
@probot-autolabeler probot-autolabeler bot added the Admin AdminBundle related issues and PRs. label Jan 29, 2025
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jan 29, 2025

Walkthrough

The pull request introduces a new prefixes variable across multiple Twig templates in the Sylius Admin Bundle. This variable is designed to provide a more dynamic approach to hook referencing, allowing for flexible prefix configurations. The changes are consistent across several CRUD (Create, Read, Update, Delete) templates, including order, product, promotion coupon, and shared templates. The implementation allows for conditional initialization of prefixes based on configuration variables, with a fallback to default values.

Changes

File Change Summary
src/Sylius/Bundle/AdminBundle/templates/order/history.html.twig Added prefixes array with 'sylius_admin.order.history' and 'sylius_admin.common'
src/Sylius/Bundle/AdminBundle/templates/product/generate_variants.html.twig Added prefixes array with 'sylius_admin.product.generate_variants' and 'sylius_admin.common'
src/Sylius/Bundle/AdminBundle/templates/promotion_coupon/generate.html.twig Added prefixes array with 'sylius_admin.promotion_coupon.generate' and 'sylius_admin.common'
src/Sylius/Bundle/AdminBundle/templates/shared/crud/... Dynamic prefixes initialization with conditional configuration and resource-based prefixes

Suggested labels

enhancement

Suggested reviewers

  • GSadee

Poem

🐰 Hooks dancing with prefixes bright,
Twig templates finding their dynamic might,
Flexibility springs like a rabbit's leap,
Configuration secrets we now can keep,
A template symphony, coded with care! 🎉

Possibly related PRs


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (7)
src/Sylius/Bundle/AdminBundle/templates/order/history.html.twig (1)

3-6: LGTM! Consider extracting common prefix to a variable.

The introduction of the prefixes array aligns well with the PR objectives. However, since 'sylius_admin.common' is used across multiple templates, consider extracting it to a variable or constant to maintain DRY principles.

src/Sylius/Bundle/AdminBundle/templates/promotion_coupon/generate.html.twig (1)

3-6: LGTM! Good use of explicit parameter naming.

The changes are consistent with other templates. The explicit naming of the resource parameter (resource: promotion) improves code clarity - this is a good practice that could be adopted in other templates as well.

Also applies to: 13-13

src/Sylius/Bundle/AdminBundle/templates/shared/crud/show.html.twig (2)

3-3: Improve readability and null safety of the prefix initialization.

The current ternary operation could be clearer and safer. Consider using a more explicit if-else block or the null coalescing operator.

-{% set prefixes = configuration.vars.hook_prefix is defined ? [configuration.vars.hook_prefix] %}
+{% set prefixes = configuration.vars.hook_prefix is defined and configuration.vars.hook_prefix is not null
+    ? [configuration.vars.hook_prefix]
+    : []
+%}

4-7: Consider adding type hints in comments.

The template uses complex data structures. Consider adding type hints in comments to improve maintainability:

+{# @var prefixes string[] #}
 {% set prefixes = prefixes|default({})|merge([
     'sylius_admin.%resource_name%'|replace({'%resource_name%': resource_name|default(metadata.name)}),
     'sylius_admin.common'
 ]) %}
src/Sylius/Bundle/AdminBundle/templates/shared/crud/create.html.twig (1)

3-7: Consider using array literal for default value

The default({}) usage suggests an object, but an empty array [] would be more appropriate since we're working with arrays.

-{% set prefixes = prefixes|default({})|merge([
+{% set prefixes = prefixes|default([])|merge([

Additionally, consider adding documentation comments to explain the prefix structure and usage.

+{# Configures hook prefixes for CRUD templates
+   - Uses custom prefix from configuration.vars.hook_prefix if defined
+   - Adds default admin prefix with resource name
+   - Includes common admin hooks
+#}
 {% set prefixes = configuration.vars.hook_prefix is defined ? [configuration.vars.hook_prefix] %}
src/Sylius/Bundle/AdminBundle/templates/shared/crud/index.html.twig (2)

8-8: Remove extra empty line

This empty line appears to be unintentional as it's not present in other templates.


3-7: Well-structured implementation for configurable hook prefixes

The implementation successfully achieves the PR objectives by:

  1. Maintaining backward compatibility with existing hook prefixes
  2. Allowing plugins/applications to customize prefixes
  3. Consistently applying the changes across all CRUD templates

The approach is clean and follows Sylius's template extension patterns.

Consider documenting this feature in:

  1. The bundle's configuration reference
  2. The template customization guide
  3. Plugin development documentation
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8281096 and 5ea607a.

📒 Files selected for processing (7)
  • src/Sylius/Bundle/AdminBundle/templates/order/history.html.twig (1 hunks)
  • src/Sylius/Bundle/AdminBundle/templates/product/generate_variants.html.twig (1 hunks)
  • src/Sylius/Bundle/AdminBundle/templates/promotion_coupon/generate.html.twig (1 hunks)
  • src/Sylius/Bundle/AdminBundle/templates/shared/crud/create.html.twig (1 hunks)
  • src/Sylius/Bundle/AdminBundle/templates/shared/crud/index.html.twig (1 hunks)
  • src/Sylius/Bundle/AdminBundle/templates/shared/crud/show.html.twig (1 hunks)
  • src/Sylius/Bundle/AdminBundle/templates/shared/crud/update.html.twig (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Static checks / PHP 8.3, Symfony ^7.1
  • GitHub Check: Static checks / PHP 8.2, Symfony ^6.4
🔇 Additional comments (4)
src/Sylius/Bundle/AdminBundle/templates/product/generate_variants.html.twig (1)

3-6: LGTM! Implementation is consistent.

The changes follow the same pattern as other templates, maintaining consistency across the codebase while enabling hook prefix configurability.

src/Sylius/Bundle/AdminBundle/templates/shared/crud/show.html.twig (1)

Line range hint 13-24: LGTM! Good separation of concerns with different hook points.

The separation of hooks for the main content, stylesheets, and javascripts is well-structured and follows good practices for template organization.

src/Sylius/Bundle/AdminBundle/templates/shared/crud/update.html.twig (1)

3-7: Maintain consistency with create.html.twig changes

The same improvements suggested for create.html.twig should be applied here for consistency:

  • Use default([]) instead of default({})
  • Add documentation comments
src/Sylius/Bundle/AdminBundle/templates/shared/crud/index.html.twig (1)

3-7: Maintain consistency with other CRUD templates

The same improvements suggested for other templates should be applied here for consistency:

  • Use default([]) instead of default({})
  • Add documentation comments

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 29, 2025

Bunnyshell Preview Environment deleted

Available commands:

  • /bns:deploy to redeploy the environment

@GSadee GSadee merged commit 40aa30f into Sylius:2.0 Jan 30, 2025
@Wojdylak Wojdylak deleted the twig-hooks-add-hook-prefix branch March 21, 2025 20:02
loic425 added a commit to Sylius/Stack that referenced this pull request Sep 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Admin AdminBundle related issues and PRs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants