How to add new layout for admin dashboard - php

In Zend Expressive, the layout is "default" into "templates" folder.
I would like to add "admin" folder into "templates" folder like that:
Templates
admin
app
admin-page.phtml
error
404.phtml
error.phtml
layout
default.phtml
default
app
home-page.phtml
error
404.phtml
error.phtml
layout
default.phtml
I've tried with the tutorials of Zend expressive to add new layout but no success for me...
class AdminPageHandler implements RequestHandlerInterface
{
private $template;
public function __construct(TemplateRendererInterface $template)
{
$this->template = $template;
}
public function handle(ServerRequestInterface $request) : ResponseInterface
{
$data = [
'admin' => 'layout::admin',
// or 'layout::admin',
// or 'layout::alternative',
];
$content = $this->template->render('pages::admin-page', $data);
return new HtmlResponse($content);
}
}
How can I add a new layout for my admin dashboard?
I would like to add new layout for my admin dashboard because the HTML script is different of my Home Application.

The template paths can be found in ConfigProvider class => __invoke method, under 'templates' => 'paths' or in getTemplates() method. There you should add a new path:
/**
* Returns the templates configuration
*/
public function getTemplates(): array
{
return [
'paths' => [
'app' => [__DIR__ . '/../templates/app'],
'error' => [__DIR__ . '/../templates/error'],
'layout' => [__DIR__ . '/../templates/layout'],
'admin' => [__DIR__ . '/../templates/admin'],
],
];
}
then your handler should look something like this
public function handle(ServerRequestInterface $request) : ResponseInterface
{
$data = [
'admin' => 'layout::admin',
// or 'layout::admin',
// or 'layout::alternative',
];
$content = $this->template->render('admin::app/admin-page', $data);
return new HtmlResponse($content);
}

Related

Override Dashboard in TYPO3

Im trying to override the help_AboutmodulesAboutmodulesiframe in TYPO3.
I think that is the initial page when you are log in as backend user.
I override the Route, so I can set an own Controller.
And in my controller I set the templatePath to my own index.html
'help_AboutmodulesAboutmodules' => [
'access' => 'public',
'target' => Controller\EditorOverviewController::class . '::getEditorOverview',
],
public function getEditorOverview()
{
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts')]);
$view->setTemplateRootPaths(
[GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/EditorOverview')]
);
$view->setTemplatePathAndFilename(
GeneralUtility::getFileAbsFileName(
'EXT:backend/Resources/Private/Templates/EditorOverview/Index.html'
)
);
$rendered = $view->render();
echo $rendered;
}
Is this the right way?
And how can I add the TYPO3 own bootstrap to the iframe or maybe javascript?

Drupal 8 custom block (module) create twig template file

I have a custom Module that creates a custom Block which has field elements.
This all works fine but I need to theme this block. I have checked the other posts on here and tried with no luck.
I have enabled twig debug and got theme suggestions. Still no luck.
Can anyone please point me in the right direction.
This is what I have so far:
my_module/my_module.module
// nothing related in here
my_module/src/Plugin/Block/myModuleBlock.php
<?php
namespace Drupal\my_module\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a 'ModuleBlock' block.
*
* #Block(
* id = "module_block",
* admin_label = #Translation("My Module"),
* )
*/
class ModuleBlock extends BlockBase {
public function blockForm($form, FormStateInterface $form_state) {
$form['test'] = array(
'#type' => 'select',
'#title' => $this->t('test'),
'#description' => $this->t('test list'),
'#options' => array(
'Test' => $this->t('Test'),
),
'#default_value' => isset($this->configuration['test']) ? $this->configuration['test'] : 'Test',
'#size' => 0,
'#weight' => '10',
'#required' => TRUE,
);
return $form;
}
/**
* {#inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['test'] = $form_state->getValue('test');
}
/**
* {#inheritdoc}
*/
public function build() {
$build = [];
$build['module_block_test']['#markup'] = '<p>' . $this->configuration['test'] . '</p>';
return $build;
}
}
my_module/templates/block--my-module.html.twig // as suggested by twig debug
<h1>This is a test</h1>
<div id="test-widget">{{ content }}</div>
I should also note that in my my_theme.theme I have this but I don;t think its relevant:
// Add content type suggestions.
function my_theme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
if ($node = \Drupal::request()->attributes->get('node')) {
array_splice($suggestions, 1, 0, 'page__node__' . $node->getType());
}
}
As for what I've tried is this:
public function build() {
return array(
'#theme' => 'block--my-module'
);
}
But still no go.
Any help here is very much appreciated.
UPDATE: So I just got it to work but I still need help. I moved the template block--my-module.html.twig to my theme directory and it worked.
How do I get it to work in my module directory?
UPDATE: So I just got it to work but I still need help. I moved the
template block--my-module.html.twig to my theme directory and it
worked.
How do I get it to work in my module directory?
You can create a directory called templates/ in your modules root.
Place your template here.
Now let Drupal know you store the template in your module.
in your_module.module add this function:
function YOUR_MODULE_theme($existing, $type, $theme, $path) {
return array(
'block__my_module' => array(
'render element' => 'elements',
'template' => 'block--my-module',
'base hook' => 'block'
)
);
}
This is not tested. It´s the way it worked for my custom block.
Don´t forget to clear the cache.
To be able to add the twig file in your module, you need to make sure the module defines the reference, not the theme.
You can still implement hook_theme() in the module's .module file as follows:
function mymodule_theme($existing, $type, $theme, $path) {
return [
'mymodule_block' => [
'variables' => [
// define defaults for any variables you want in the twig file
'attributes' => [
'class' => ['my-module-class'],
], //etc
],
],
];
}
Then in your block's build() implementation you can add a reference to the new theme function:
public function build() {
// Load the configuration from the form
$config = $this->getConfiguration();
$test_value = isset($config['test']) ? $config['test'] : '';
$build = [];
$build['#theme'] = 'mymodule_block';
// You would not do both of these things...
$build['#test_value'] = $test_value;
$build['module_block_test']['#markup'] = '<p>' . $test_value . '</p>';
return $build;
}
Finally be careful about where you place your twig file and what you name it. Create a templates directory in your module directory, and replace the _ in the theme function name with -: mymodule-block.html.twig

Yii2 Custom http exceptions views

In the application login I have the following code that throw ...HttpException on logging errors:
// common/models/LoginForm.php which is called from the backend SiteController actionLogin method as $model = new LoginForm();
public function loginAdmin()
{
//die($this->getUser()->getRoleValue()."hhh");
if ($this->getUser()->getRoleValue() >= ValueHelpers::getRoleValue('Admin') && $this->getUser()->getStatusValue() == ValueHelpers::getStatusValue('Active')){
if ($this->validate()){
return \Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30:0);
}
else{
throw new \yii\web\NotFoundHttpException('Incorrect Password or Username.');
}
}
else{
throw new \yii\web\ForbiddenHttpException('Insufficient privileges to access this area.');
}
}
It is working fine, but I want to customize the page the rendered with each of NotFoundHttpException and ForbiddenHttpException. I tried to search the Yii2 api to find any parameters that may define view in the construct of the object but I could not find that. So, is there any way to custom the view of the exception?
From Mihai P. (Thank you) answer, I have got this answer. I opened the file of the error class at vendor\yiisoft\yii2\web\ErrorAction.php and I have found that it has a public property for view, so, I decided to use it and hence I defined it in the error array of the actions method as follows:
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
'view' => '#common/views/error.php',
],
];
}
Finally, in common folder I had to create a new folder named views and fill it with a view file called error.php with the following simple code
<?php
$this->title = $name;
echo $name;
echo "<br>";
echo $message;
echo "<br>";
echo $exception;
The three variables in the view $name, $message and $exception are supplied from the ErrorAction object and they could be found in the last lines of that file
...
else {
return $this->controller->render($this->view ?: $this->id, [
'name' => $name,
'message' => $message,
'exception' => $exception,
]);
}
...
If you take a look here https://github.com/yiisoft/yii2-app-advanced/blob/master/frontend/controllers/SiteController.php
You can see that it uses an external action to handle the errors
/**
* #inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
You can either create your own ErrorAction file that extends the default one and use yours instead of the default one from Yii, or just comment out that action and create a normal actionError and put it in there.

Set a generic layout for all modules in Zend framework 2

I'm working on a ZF2 Project, and I have some modules in my directories :
/module/module1
/module/module2
/module/module3
/module/module4
[...]
But, in each module I also have a specific layout, respectively :
/module/module1/view/layout/layout.phtml
/module/module2/view/layout/layout.phtml
/module/module3/view/layout/layout.phtml
/module/module4/view/layout/layout.phtml
My question is : How can I set a generic layout for all my modules without to have to modify each layout when I want.
Thank you
You can set the layout to be what ever you want in each modules config, just change the layout to be what ever you want:
module.config.php or inside getConfig()
'view_manager' => array(
// other stuff here..
'template_map' => array(
// use Applications layout instead
'layout/layout' => __DIR__ . '/../Application/view/application/layout/layout.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
Or you can set each module to selectively set it's layout in Module.php:
Module.php
/**
* Initialize
*/
public function init(ModuleManager $manager)
{
$events = $manager->getEventManager();
$sharedEvents = $events->getSharedManager();
$sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
/* #var $e \Zend\Mvc\MvcEvent */
// fired when an ActionController under the namespace is dispatched.
$controller = $e->getTarget();
$routeMatch = $e->getRouteMatch();
/* #var $routeMatch \Zend\Mvc\Router\RouteMatch */
$routeName = $routeMatch->getMatchedRouteName();
$controller->layout('application/layout/layout');
}, 100);
}

Zend_Navigation add class to active link

How can I add a class to the active navigation link? If a link points to URI /index/index and the request URI is also /index/index, I would like the link to have class, for example:
<li class="active">
Index
</li>
This is how I am initializing navigation in the bootstrap:
protected function _initNavigation()
{
$navigation = new Zend_Navigation($this->getOption('navigation'));
$this->view->navigation($navigation);
}
Ok,
I have solved this by writing a controller plugin:
<?php
class My_Controller_Plugin_PrepareNavigation extends Zend_Controller_Plugin_Abstract
{
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
$viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer');
$viewRenderer->initView();
$view = $viewRenderer->view;
$container = new Zend_Navigation(Zend_Registry::get('configuration')->navigation);
foreach ($container->getPages() as $page) {
$uri = $page->getHref();
if ($uri === $request->getRequestUri()) {
$page->setClass('active');
}
}
$view->navigation($container);
}
}
This is how to create a navigation() in a layout() with zend frameworks using Application.
Well, at least one way of doing it. the CSS class is set on the
put this into the Bootstrap.php file:
protected function _initNavigation()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
include APPLICATION_PATH . '/layouts/scripts/menu.phtml';
$view->navigation($container);
}
This allows you to create an array for a menu in the file menu.phtml, so that you can still maintain the active class on the current link. For some strange reason, if you use this you must include the controller property in the array to get the CSS active class on the current link.
put something like this into the /layouts/scripts/menu.phtml file:
$container = new Zend_Navigation(array(
array(
'label' => 'HOME',
'id' => 'tasks',
'uri'=>'/',
'controller' => 'Index'
),
array(
'label' => 'Contact',
'uri' => 'contact',
'controller' => 'Contact'
),
.... more code here ...
put this into the layout.phtml file:
$options = array('ulClass' => 'menu');

Categories