Yii set default options for a custom CGridView - php

Using PHP framework Yii. As you know, default CGridView table CSS class is items. Well I want to change this value. I know it's possible for one specific widget. Like this:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'columns'=>array(),
'itemsCssClass'=>'gridtablecss',
)); ?>
But how to do this for whole Yii application? I mean make default some another class not items

If you only want to set some static parameters for each widget then you don't need to extend the class. You can also use Yii's widgetFactory component. You can configure it in your main.php configuration file.
'components' => array(
// ...
// Default properties for some widgets
'widgetFactory' => array(
'widgets' => array(
'CGridView' => array(
'itemsCssClass' => 'gridtablecss'
),
),
),
),

Create another php file in your extensions folder, and name it MyGridView.php:
<?php
Yii::import('zii.widgets.grid.CGridView');
class MyGridView extends CGridView {
public function init() {
// Set default CSS class
$this->itemsCssClass = 'gridtablecss';
// Other modifications, i.e.: Increase page size
if ($this->dataProvider !== null)
$this->dataProvider->pagination->pageSize = 999999;
parent::init();
}
}
Now instead of using CGridView, you use your new CGridView like this:
<?php $this->widget('MyGridView', array(
Make sure your main.php config file is importing extensions:
'import' => array(
'application.extensions.*',
...
),
You can also extend CListView (and other similar core Yii classes) the same way.

Related

Extending page table with field using itemsProcFunc in TYPO3 6.2

I'm trying to extend the 'page' properties form by extending the page db table and TCA array from within an extension. This works, except that my custom function won't be called. If I replace my own itemsProcFunc line with a TYPO3 core function itemsProcFunc line it works, but with my own function it never works (I just get an empty result/selectlist, even when I simply return a dummy array: "return array('title','1');"....
Here's my code in my extension's ext_tables.php:
<?php
$TCA['pages']['columns'] += array(
'targetelement' => array(
'exclude' => 0,
'label' => 'Target element (first select a target page!)',
'config' => array (
'type' => 'select',
'items' => Array (
Array('',0),
),
'size' => 1,
'minitems' => 1,
'maxitems' => 1,
//'itemsProcFunc' => 'TYPO3\CMS\Backend\View\BackendLayoutView->addBackendLayoutItems',
'itemsProcFunc' => 'Vendor\Myextension\Controller\Hooks\CustomTargetElementSelector->getContentElements',
),
)
);
t3lib_extMgm::addToAllTCAtypes('pages', 'targetelement,', '2', 'after:nav_title');
t3lib_extMgm::addToAllTCAtypes('pages', 'targetelement', '1,5,4,199,254', 'after:title');
P.s. I replace Vendor\Myextension for my own namespace of course.
I don't know where to put my function file exactly, I assume in extension\Classes\Controllers\Hooks\CustomTargetElementSelector.php.
My ultimate goal is to display a list of content elements of the selected shortcut page UID..
P.s.2 my CustomTargetElementSelect.php file looks like this (contents just return a single item, dummy list result:
<?php
namespace Vendor\Myextension\Controller;
class CustomTargetElementsSelector extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
public function getContentElements(array &$params,$pObj){
return array('title','uid');
}
}
First of all, an itemsProcFunc should be a simple class; I never tested if an Extbase controller context is available in an itemsProcFunc.
Your hook should (this is just a recommendation) reside in
yourext/Classes/Hook/CustomTargetElementSelector.php
Namespace:
namespace Vendor\Yourext\Hook;
class CustomTargetElementSelector {
[method inside]
}
After flushing the system cache, if the hook still has no function, set a die() statement within the function to find out if the function is called at all. Currently it cannot work because the location of your class (Controllers/Hooks) and the namespace (Controller) don't fit.
For the sake of full 6.2/7 compatibility, replace
t3lib_extMgm::
by
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::

Zend2 navigation

I would like to make navigation buttons in my view, for example index.phtml but it's not working. I did know how to do it in Zend1 but in Zend2 I have a problem. My code looks like this (file index.phtml):
$container = new \Zend\Navigation\Navigation($tableActions);
var_dump($container);
echo '<div class="table-column">';
echo $this->navigation($container)->menu();
echo '</div>';
Variable $tableAction looks like this:
public $tableActions = array(
array(
'label' => 'On/Off',
'module' => 'import',
'controller' => 'import',
'action' => 'setstatus',
'params' => array('id' => null),
),
);
I did not get any error, just whole site die on this line. var_dump returns object(Zend\Navigation\Navigation) so it's fine so far. Problem is, how to show it...
The navigation pages have dependencies which aren't being met by just creating a new container class in a view. The Mvc page needs a RouteStackInterface (Router) instance and a RouteMatch instance. Similarly Uri pages need the current Request instance.
You can see this clearly if you take a look at the Zend\Navigation\Service\AbstractNavigationFactory and its preparePages and injectComponents methods.
The view is not the right place to be instantiating menus, instead put the menu configuration spec in your module.config.php...
<?php
return array(
'navigation' => array(
'table_actions' => array(
array(
'label' => 'On/Off',
'module' => 'import',
'controller' => 'import',
'action' => 'setstatus',
'params' => array('id' => null),
),
),
),
);
Write a factory extending the AbstractNavigationFactory class and implement the getName() method which returns the name of your menu spec key (table_actions in this example)
<?php
namespace Application\Navigation\Service;
use Zend\Navigation\Service\AbstractNavigationFactory;
class TableActionsFactory extends AbstractNavigationFactory
{
/**
* #return string
*/
protected function getName()
{
return 'table_actions';
}
}
Map the factory to a service name in the service_manager spec of module.config.php ...
<?php
return array(
'navigation' => array(// as above ... )
'service_manager' => array(
'factories' => array(
'TableActionsMenu' => 'Application\Navigation\Service\TableActionsFactory',
),
),
);
Now you can call the view helper using the service name TableActionsMenu you just mapped
<div class="table-column">
<?php echo $this->navigation('TableActionsMenu')->menu(); ?>
</div>
Finally, if, as I suspect, you need to change an attribute of the page depending on the view, you can do that too, navigation containers have find* methods which can be accessed from the navigation helper and used to retrieve pages.
Here's an example looking for the page with a matching page label, then changing it before rendering (obviously not an ideal search param, but it gives you the idea)
$page = $this->navigation('TableActionsMenu')->findOneByLabel('On/Off');
$page->setLabel('Off/On');
// and then render ...
echo $this->navigation('TableActionsMenu')->menu();

Set Different Layout for Different Module in Zend Framework 2?

I have two module Student and Teacher.
I also have two different layout one is studentlayout.phtml and another is teacherlayout.phtml
How can I set studentlayout for Student module and teacherlayout for Teachermodule?
As Per Sam's answer .Thanks Its working fine.
but i also want to set two different layout For Teacher.
So i add following code in my main config file for project:
'module_layouts' => array(
'Teacher' => array(
'default' => 'layout/adminlayout',
'login' => 'layout/loginlayout',
),
'Student' => 'layout/studentlayout',
),
My module.config.php file for teacher module:
'module_layouts' => array(
'Teacher' => array(
'default' => 'layout/adminlayout',
'login' => 'layout/loginlayout',
),
'Student' => 'layout/studentlayout',
),
But all time all action of Teacher module take adminlayout. why login action can't take loginlayout?its ovveride?
Usage
Using EdpModuleLayouts is very, very simple. In any module config or autoloaded config file simply specify the following:
array(
'module_layouts' => array(
'Teacher' => 'layout/teacher',
'Student' => 'layout/student'
),
);
That's it! Of course you need to define those layouts, too... just check Application Modules module.config.php to see how to define a layout.
If you only want to change layout for your one action you can use layout() plugin in your controllers action, or if you want different layout for all actions in one controller only in your module you can do it in bootstrap:
public function onBootstrap(\Zend\EventManager\EventInterface $e) {
$eventManager = $e->getApplication()->getEventManager();
$sharedEventManager = $eventManager->getSharedManager();
$sharedEventManager->attach('Auth\Controller\AuthController', \Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'));
}
public function onDispatch(MvcEvent $e) {
$controller = $e->getTarget();
$controller->layout('layout/loginLayout');
}
After each action in that controller you will change root ViewModel layout you can go further and specify here more controllers where you want your layout like this
$sharedEventManager>attach(array('Auth\Controller\AuthController',
'Auth\Controller\Registration'),
\Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'));
}

ZF2 Routing configuration via RouteProviderInterface

I have been trying to configure our Module.php to use the Module Manager Listeners for configuration (i.e interfaces that are available under Zend\ModuleManager\Feature\*). Specifically, I want to be able to configure the routes of my module outside of the main module.config.php. I have not been able to find any actual examples of this.
What I have found, if I have read the documentation correctly, is that the method getRouteConfig() should merge in my routes into the array provided by getConfig()?
Module.php
class Module implements Feature\RouteProviderInterface
{
//...
public function getRouteConfig()
{
return include __DIR__ . '/config/route.config.php';
}
//...
}
/config/route.config.php
return array(
'route_manager' => array(
'router' => array (
'routes' => array(
//.. routes that were working correctly when added to module.config.php
),
),
),
);
I can see the array returned via getRouteConfig() so I know the method is being called correctly.
Perhaps I am misunderstanding the purpose of the above interface, or I have not provided the correct "key" (route_manager) for this to be merged correctly, as I'm getting 404 for my routes.
Any help would be appreciated!
I haven't done this in the way you mentioned yet, but the key route_manager is not required within the getRouteConfig() Method.
This is due to the fact that all of the get{$specificManager}Config()-Methods are called directly from their respective Manager-Classes. Therefore the initial key is not required. Using another terminology, when using getRouteConfig() you are already in the scope of route_manager. Same as when you use getServiceConfig() you're already in the scope of service_manager. However getConfig() is within the application-scope and therefore accessing configuration of application-parts, you need to address tose specificaly.
One thing to note is: the configuration of getConfig() can be cached to increase performance, whereas all the other get{$specificManager}Config() methods are not. Especially in the case of the RouteConfiguration I'd highly suggest to use the getConfig()-Method for your RouteConfig.
If you really need to separate the configuration, then I'd suggest the way that #Hendriq displayed for you.
Well I have it working but I only use the getConfig(). What is do is I use an array_merge in the getConfig().
public function getConfig()
{
return array_merge(
require_once 'path_to_config/module.config.php',
require_once 'path_to_config/routes.config.php'
);
}
My router.config.php looks then like:
return [
'router' => [
'routes' => [
// routes
]
]
];
This way I also got some other config files seperated (ACL).
Edit
Thanks to the article Understanding ZF2-Configuration, I got an idea. I think your array should not be:
return array(
'route_manager' => array(
'router' => array (
'routes' => array(
//.. routes that were working correctly when added to module.config.php
)
)
)
);
but rather be
return array(
'router' => array (
'routes' => array(
//.. routes that were working correctly when added to module.config.php
),
),
);
The getRouteConfig is similar to the other providers it is there so you're able to create some custom routes. I guess what you're trying to do is most appropiate through hendriq's method.
An example of getRouteConfigcan be found at http://zf2cheatsheet.com/
public function getRouteConfig()
{
return array(
'factories' => array(
'pageRoute' => function ($routePluginManager) {
$locator = $routePluginManager->getServiceLocator();
$params = array('defaults' => array('controller' => 'routeTest','action' => 'page','id' => 'pages'));
$route = Route\PageRoute::factory($params);
$route->setServiceManager($locator);
return $route;
},
),
);
}
In our Module\Route namespace we create the class PageRoute which implements Zend\Mvc\Http\RouteInterface and, in our specific case for the example, Zend\ServiceManager\ServiceManagerAwareInterface. Now just implement the functions of the interface... In the sample he uses Doctrine to load the pages from the database.
Finally we can add our new custom route to our module.config.php so it can be used:
'page' => array(
'type' => 'pageRoute',
),
As you can see in this last step we go back to Hendriq's solution as the intended use is not to load the routes into the router, but creating custom routes.
Hope this helps

Yii Url Parameter Automatically placed in URL's

Hello (sorry for my english...)
I got an aplication in Yii. I choose diffrent databases depending on $_GET['project']. My urls looks like index.php?r=controler/action&project=MyProject.
But i have to add &project=.. to every single link on my site, how can i make Yii do it automatically?
If you are using CUrlManager::createUrl() (or one of the other createUrl() variants) to create your links, you could override it in your own custom UrlManager:
class UrlManager extends CUrlManager {
public function createUrl($route, $params=array(), $ampersand='&') {
isset($params['project']) || $params['project'] = 'MyProject';
return parent::createUrl($route, $params, $ampersand);
}
}
Then in your config be sure to use your own custom UrlManager class:
return array(
...
'components' => array(
'urlManager' => array(
'class' => 'UrlManager',
),
),
...
);

Categories