Custom Data (Pro)
Last updated
Was this helpful?
Last updated
Was this helpful?
Using the Custom Data feature you can add extra arguments to the Card query, and extra variables to the Card template.
Card provides a user interface for all the main . However, to avoid overloading the user interface, we haven't included all possible arguments.
In advanced cases, you may need to use arguments that are missing from the UI. In the Pro Edition, you can achieve this by using the Custom Data feature, which can be found under the Advanced tab of your Card settings.
This feature allows you to extend the current Card by merging them with arguments returned from a custom PHP snippet. The data return should return an associative array, which will be merged with the settings from the UI.
Using this feature, you can also add any custom variables to your Card template.
The field with the snippet can be found in the Advanced tab of your Card.
The snippet is a PHP code, which must return an instance of the Custom_Card_Data
class.
using the get_variables()
method you can pass custom variables to the Card template
using the get_query_arguments()
method you can pass custom arguments to the Card query
using the get_default_variables()
method you can access to the default twig variables (that filled out automatically by the plugin)
using the get_default_query_arguments()
method you can access to the default query arguments (that filled out automatically by the plugin)
using the get_container()
method you can access the PHP-DI Container (see the related chapter of this page)
A simple example is below:
You won't see a significant benefit if your MyClass
has no dependencies. But let's say the class has 2-3 constructor arguments (e.g., a logger and a theme object). To get the MyClass
instance, you would need to create those instances initially. Furthermore, one of those arguments may have its own arguments, and your creation code will turn into a multi-line creation.
Additionally, PHP-DI supports and encourages the use of interfaces, so you can call
->get(MapInterface::class)->calculate()
in your code, while defining which exact class should be used for each interface in the container configuration.
using the get_ajax_response()
method you define the Ajax callback for the block.
using the get_custom_arguments()
method you can access the . The field is available in the both methods above.
is a well-known dependency injection container. If you haven't used it, we recommend checking it out and harnessing its capabilities.
Advanced Views supports PHP-DI out-of-the-box. To simplify access to your theme's PHP class instances inside the Custom_Data instances, you can employ the PHP-DI container. To do this, you need to define the container in your theme using the hook. Then, you can access it using the get_container()
method in any method of the Custom_Data class.
If you're unfamiliar with , the benefits of the code above may be unclear to you. We could replace $container->get
with new MyClass()
and it would work perfectly. However, the container is a powerful tool, especially for more complex cases.
With PHP-DI, you don't need to worry about the constructor, as PHP-DI will use to get the necessary instances and pass them to the constructor on the fly.
An additional benefit is that the ->get
method, ->make
, creates the instance only once, reusing it for other calls. This way, you get a smart 'global' variables pool, allowing you to handle dependencies easily and efficiently.
Unlike the , which is , it doesn't turn your code into single-instance classes. You still define constructors, clearly specifying class dependencies, while avoiding the hassle of manual creation.