How should I apply custom styles to Vendor Module's Form or embed it on my own View script? More specifically I want to work with forms provided by EdpUser Module (https://github.com/EvanDotPro/EdpUser).
The easiest way seems to be modifying the vendor's script directly. But obviously this will conflict with vendor's future updates.
Another way seems to be copying Vendor's Controller into my own Module then provide my own views. However this suffers a similar issue of getting out of sync with Vendor's future updates. And I will have to be careful with namespace.
Maybe I should extend Vendor's Controller instead of copying it. This should work well with namespaces and I should be able to access the Forms within my Controller quite easily. While using my own view scripts. Is this the right way or is there a better one?
Thanks
It is not the recommended approach to modify anything in the module's directory, especially those modules put in /vendor/
. This is also the reason configuration templates must be copied to your own /config/autoload/
directory.
A module must provide sufficient extension points to make it flexible enough for your usage. Among those possibilities are the following options:
The ZfcUser module registers itself under the /user url. If you want to change that into /account for example, you can simply add this to your configuration:
<?php
return array(
'di' => array(
'instance' => array(
'Zend\Mvc\Router\RouteStack' => array(
'parameters' => array(
'routes' => array(
'zfcuser' => array(
'options' => array(
'route' => '/account',
),
),
),
),
),
),
),
);
If you are not happy the ZfcUser\Controller\UserController
is used and you want to override some action in this controller, you can create a custom controller, for example MyUser\Controller\UserController
. If you extend the ZfcUser\Controller\UserController
and provide this configuration, you are ready to go:
<?php
return array(
'di' => array(
'instance' => array(
'Zend\Mvc\Router\RouteStack' => array(
'parameters' => array(
'routes' => array(
'zfcuser' => array(
'options' => array(
'defaults' => array(
'controller' => 'MyUser\Controller\UserController'
),
),
),
),
),
),
),
),
);
You can also use DI aliasing to override the zfcuser
alias in your DI configuration:
<?php
return array(
'di' => array(
'instance' => array(
'alias' => array(
'zfcuser' => 'MyUser\Controller\UserController'
),
),
),
);
For example the ZfcUser module triggers several events to help other modules hook into the form creation process. Both the ZfcUser\Form\Login
as ZfcUser\Form\Register
trigger an init
event after they set up all form elements. This gives you the chance to add or remove elements.
use Zend\EventManager\StaticEventManager;
$events = StaticEventManager::getInstance();
$events->attach('ZfcUser\Form\Login', 'init', function ($e) {
$form = $e->getTarget();
$form->addElement('text', 'something-new');
});
I need to make two remarks here:
init
on both forms, but this is not a given for every module. You must look into the documentation or source code to find out about this.Zend\EventManager
is currently under a refactoring to replace the singleton StaticEventManager
by a SharedEventManager
instance which can be instantiated through the Zend\Di
locator. You can watch the progress of this refactoring in this Pull Request.As per this RFC the Zend\Form
component is likely to change. Especially for the rendering, the decorators will be removed and "just" normal view helpers will render the form instead. With this given, plus the possibility to override views, it is very easy to change the rendering of an action just by using another module with only some view scripts.
If you create your own module and provide this configuration, you add a new location to the template path stack:
<?php
return array(
'di' => array(
'instance' => array(
'Zend\View\Resolver\TemplatePathStack' => array(
'parameters' => array(
'paths' => array(
'myuser' => __DIR__ . '/../view',
),
),
),
),
),
);
Now you can create view scripts in your module's view
directory. If you have your module called "MyUser" and located under /modules/MyUser
and you want to override the login view script from ZfcUser (located in /vendor/ZfcUser/view/zfcuser/login.phtml
, create your view script in /modules/MyUser/view/zfcuser/login.phtml
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With