Cannot use models in the config folder in Laravel - php

I have a plugin that generates a navbar using PHP. The folder is in project/config/menu.php
It looks like this:
<?php
return [
//HORIZONTAL MENU LAYOUT - MENU
'horizontal' => [
[
'title' => 'bar',
'link' => '/bar/all',
'active' => 'bar*',
'icon' => 'fa fa-sign-in',
],
[
'title' => 'foo',
'link' => '/foo/all',
'active' => 'foo*',
'icon' => 'fa fa-sign-out',
],
]
];
I want to add some model information.
This is my attempt:
<?php
use Auth;
$id = Auth::user()->id;
return [
//HORIZONTAL MENU LAYOUT - MENU
'horizontal' => [
[
'title' => 'bar',
'link' => '/bar/'. $id,
'active' => 'bar*',
'icon' => 'fa fa-sign-in',
],
[
'title' => 'foo',
'link' => '/foo/all',
'active' => 'foo*',
'icon' => 'fa fa-sign-out',
],
]
];
I get this error: Class 'Auth' not found. I have also tried with models:
$model = \App\Model::count();
Which gives me this error:
Call to a member function connection() on null
How do I use these models here?

laravel config loads before any other things, so instantiating model will give an error, and that error you are getting is due to no database connection information loaded during this specific config file loads. I wonder why you need to call model in config, you can simply build something like templates of menu layout like below:
<?php
return [
//HORIZONTAL MENU LAYOUT - MENU
horizontal' => [
[
'title' => 'bar',
'link' => '/bar/%d', // here %d is userId from database
'active' => 'bar*',
'icon' => 'fa fa-sign-in',
],
[
'title' => 'foo',
'link' => '/foo/all',
'active' => 'foo*',
'icon' => 'fa fa-sign-out',
],
]
];
and later replace that %d with value from model.

Related

TYPO3 How to add Create new button on TCA?

I would like to build a slider (Own Content Element). So on the backend i would like to have a section with multiple records. What do i mean? By clicking the "Create new" i would like to have an image selection and rich text selection.
Something like that.
How can i achieve this?
So far i have:
TCA/Overrides/tt_content.php
which gives me the rich editor and the image selection in backend, but not grouped.
$GLOBALS['TCA']['tt_content']['types']['my_slider'] = array( 'showitem' => '
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:general,
--palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:palette.general;general,
--palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:palette.headers;headers,bodytext,assets;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.media,
',
'columnsOverrides' => [
'bodytext' => [
'config' => [
'enableRichtext' => true,
'richtextConfiguration' => 'default'
]
]
]
);
tt_content.typosript
tt_content {
my_slider < lib.contentElement
my_slider {
templateRootPaths.10 = {$Private}Templates/
templateName = Slider.html
dataProcessing {
10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
10 {
references.fieldName = assets
as = images
}
}
}
}
Slider.html
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
data-namespace-typo3-fluid="true">
<h1>{data.header}</h1>
<p>{data.bodytext}</p>
<f:for each="{images}" as="image">
<f:image image="{image}" alt="{file.properties.alt}" cropVariant="desktop"/>
{image.description}
</f:for>
<f:debug>{data}</f:debug>
</html>
Now with the current code i get the results in frontend. One Text and one Image. But how can i get it as group after configured it on the backend?
You need add new field to the sys_file_reference TCA:
Configuration/TCA/Overrides/sys_file_reference.php
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
'sys_file_reference',
[
'tx_myext_description' => [
'label' => 'My field description',
'config' => [
'type' => 'text',
'cols' => '80',
'rows' => '15',
'enableRichtext' => true,
'richtextConfiguration' => 'default'
]
],
]
);
ext_tables.sql
CREATE TABLE sys_file_reference (
tx_myext_description mediumtext,
);
Remember to add new field into database (using database compare tool in install tool).
Then use it in TCA of your new slider:
Configuration/TCA/Overrides/tt_content.php
$GLOBALS['TCA']['tt_content']['types']['my_slider'] = [
'showitem' => '
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:general,
--palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:palette.general;general,
--palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:palette.headers;headers,bodytext,assets;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.media,
',
'columnsOverrides' => [
'bodytext' => [
'config' => [
'enableRichtext' => true,
'richtextConfiguration' => 'default'
]
],
'assets' => [
'config' => [
'overrideChildTca' => [
'types' => [
0 => ['showitem' => $GLOBALS['TCA']['sys_file_reference']['types'][0]['showitem'].',tx_myext_description'],
\TYPO3\CMS\Core\Resource\File::FILETYPE_TEXT => [
'showitem' => $GLOBALS['TCA']['sys_file_reference']['types'][\TYPO3\CMS\Core\Resource\File::FILETYPE_TEXT]['showitem'].',tx_myext_description'
],
\TYPO3\CMS\Core\Resource\File::FILETYPE_IMAGE => [
'showitem' => $GLOBALS['TCA']['sys_file_reference']['types'][\TYPO3\CMS\Core\Resource\File::FILETYPE_IMAGE]['showitem'].',tx_myext_description'
],
\TYPO3\CMS\Core\Resource\File::FILETYPE_AUDIO => [
'showitem' => $GLOBALS['TCA']['sys_file_reference']['types'][\TYPO3\CMS\Core\Resource\File::FILETYPE_AUDIO]['showitem'].',tx_myext_description'
],
\TYPO3\CMS\Core\Resource\File::FILETYPE_VIDEO => [
'showitem' => $GLOBALS['TCA']['sys_file_reference']['types'][\TYPO3\CMS\Core\Resource\File::FILETYPE_VIDEO]['showitem'].',tx_myext_description'
],
\TYPO3\CMS\Core\Resource\File::FILETYPE_APPLICATION => [
'showitem' => $GLOBALS['TCA']['sys_file_reference']['types'][\TYPO3\CMS\Core\Resource\File::FILETYPE_APPLICATION]['showitem'].',tx_myext_description'
],
],
],
],
],
],
];
And then your Fluid template may look like that:
Slider.html
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
data-namespace-typo3-fluid="true">
<h1>{data.header}</h1>
<p>{data.bodytext}</p>
<f:for each="{images}" as="image">
<f:image image="{image}" alt="{file.properties.alt}" cropVariant="desktop"/>
{image.properties.tx_myext_description -> f:format.html()}
</f:for>
</html>

Yii2 ListView custom data-attribute based on currend DP model

I need to set custom data-attribute to the ListView elements. As example I try to get current DataProvider model id. But I still view error "htmlspecialchars() expects parameter 1 to be string, object given". Please check my code and halp me - how I should get this ID?
<?= ListView::widget([
'dataProvider' => $photoProvider,
'id' => 'photo-list',
'itemView' => '_photoListItem',
'viewParams' => [
'fullView' => true,
],
'options' => [
'tag' => 'ul',
'class' => 'list-view'
],
'itemOptions' => [
'tag' => 'li',
'class' => 'item',
'data' =>[
'test' => function ($model, $key, $index, $widget) {
return Html::encode($model->id);
}
]
],
'pager' => [
'class' => ScrollPager::className(),
'container' => '#photo-list',
'item' => '.item',
'triggerText' => '<div class="btn more">Load More</div>',
'noneLeftText' => '',
'triggerOffset' => 2,
'negativeMargin' => 200
],
'layout' => "{items}\n{pager}",
]);
?>
This is not possible in the ListView, according to the doc:
'data' => [
'test' => function ($model, $key, $index, $widget) {
return Html::encode($model->id);
}
]
You cannot use a function here. Only static values are allowed.
Alternatively, you can provide those attributes in tags of your _photoListItem view file.

Adding oembed plugin to 2amigos ckeditor widget in yii2

I'm trying to add the oembed plugin to ckeditor via the 2amigos widget for yii2.
I did the following, but can't seem to get the button to appear:
I downloaded and copied the oembed and widget plugins to the ckeditor plugins folder
I set the clientOptions for the widget in my form using the code below:
$form->field($theComment, 'comment')->widget(CKEditor::className(), [
'clientOptions' => ['config.extraPlugins' => 'widget,oembed'],
'options' => ['rows' => 6],
'preset' => 'full'
])
I altered the full.php preset filworkinge by adding the following lines of code below:
'toolbarGroups' => [
['name' => 'document', 'groups' => ['mode', 'document', 'doctools']],
['name' => 'clipboard', 'groups' => ['clipboard', 'undo']],
['name' => 'editing', 'groups' => [ 'find', 'selection', 'spellchecker']],
['name' => 'forms'],
'/',
['name' => 'basicstyles', 'groups' => ['basicstyles', 'colors','cleanup']],
['name' => 'paragraph', 'groups' => [ 'list', 'indent', 'blocks', 'align', 'bidi' ]],
['name' => 'links'],
['name' => 'insert'],
'/',
['name' => 'styles'],
['name' => 'blocks'],
['name' => 'colors'],
['name' => 'tools'],
['name' => 'others'],
'/', //added by me
['name' => 'oembed'], //added by me
],
Not entirely sure if the error is in my edit to clientOptions, or in my edit to toolbarGroups, or both!. Any thoughts on what I'm doing wrong would be greatly appreciated.
Thanks!
plugin add in assets folder (example plugin spoiler) zarnica.dev\www\assets\dff59c5c\plugins\spoiler
<script>CKEDITOR.config.extraPlugins = 'spoiler';</script>
DONE!

Add custom RBAC rule to PhpManager in Yii 2.0

I want to add a custom rule to PhpManager RBAC in Yii 2.0.
Here is the custom rule (#app/rbac/OwnerRule.php):
<?php
namespace app\rbac;
use yii\rbac\Rule;
/**
* Checks if userID matches user passed via params
*/
class OwnerRule extends Rule
{
public $name = 'isOwner';
public function execute($user, $item, $params)
{
$access = false;
if(isset($params['id'])){
// My custom logic used to set $access
}
return $access;
}
}
Here is the RBAC hierarchy file (#app/data/rbac.php)
<?php
use yii\rbac\Item;
return [
'manageThing0' => ['type' => Item::TYPE_OPERATION, 'description' => '...', 'bizRule' => NULL, 'data' => NULL],
'manageThing1' => ['type' => Item::TYPE_OPERATION, 'description' => '...', 'bizRule' => NULL, 'data' => NULL],
'manageThing2' => ['type' => Item::TYPE_OPERATION, 'description' => '...', 'bizRule' => NULL, 'data' => NULL],
// AND THE ROLES
'guest' => [
'type' => Item::TYPE_ROLE,
'description' => 'Guest',
'bizRule' => NULL,
'data' => NULL
],
'user' => [
'type' => Item::TYPE_ROLE,
'description' => 'User',
'children' => [
'guest',
'manageThing0', // User can edit thing0
],
'bizRule' => 'return !Yii::$app->user->isGuest;',
'data' => NULL
],
'moderator' => [
'type' => Item::TYPE_ROLE,
'description' => 'Moderator',
'children' => [
'user', // Can manage all that user can
'manageThing1', // and also thing1
],
'bizRule' => NULL,
'data' => NULL
],
'admin' => [
'type' => Item::TYPE_ROLE,
'description' => 'Admin',
'children' => [
'moderator', // can do all the stuff that moderator can
'manageThing2', // and also manage thing2
],
'bizRule' => NULL,
'data' => NULL
],
];
How do I use my custom rule in the hierarchy file?
See these links hope you will find what you are looking for,
http://www.yiiframework.com/doc-2.0/guide-security-authorization.html
http://yii2-user.dmeroff.ru/docs/custom-access-control
RBAC for basic yii2 template

OctoberCMS Sidebar not rendering

I have been trying to get familiar with octobercms, but I've come across an issue I can't seem to resolve. I have a backend controller setup with views etc. Everything works, except that the sidebar isn't loading. Also the tab isn't getting the active state.
http://gyazo.com/25e019c1db34d5807c05ebb4b3277ac7
It should look something like this:
http://gyazo.com/c71a1e1dec7c1e6b81136b313b32da47
Here is a gist with my code: https://gist.github.com/muuknl/fedb8434219c7dbe5d04
If I forgot to give certain information, please let me know and thanks in advance for the help.
here is simple solution
in controller you need to write
BackendMenu::setContext('Archetypics.Team', 'website', 'team');
refer this https://octobercms.com/docs/backend/controllers-views-ajax#navigation-context
BackendMenu::setContext('Author.Plugin name', 'Menu code', 'Sub menu code');
you need to write same thing what you have written in plugin.php in registerNavigation() function
public function registerNavigation()
{
return [
// menu code
'website' => [
'label' => 'Website',
'url' => Backend::url('muukrls/archetypics/team'),
'icon' => 'icon-pencil',
'permissions' => ['archetypics.*'],
'order' => 500,
'sideMenu' => [
'home' => [
'label' => 'Homepage',
'icon' => 'icon-copy',
'url' => Backend::url('muukrls/archetypics/home'),
'permissions' => ['archetypics.home_access'],
],
'about' => [
'label' => 'About Page',
'icon' => 'icon-list-ul',
'url' => Backend::url('muukrls/archetypics/about'),
'permissions' => ['archetypics.about_access'],
],
// sub menu code
'team' => [
'label' => 'Team Members',
'icon' => 'icon-users',
'url' => Backend::url('muukrls/archetypics/team'),
'permissions' => ['archetypics.team_access']
]
]
]
];
}

Categories