Blade Recursive, increase the depth for padding - php

I want to increase the depth int each time children are in a item so I can pad out the a tag.
Below is my code and I assumed this would work, however the number stays at 1 even though I have got children within my one of the items within $items.
sidebar.blade.php
<nav class="flex flex-col mt-10">
<ul class="pl-4">
#foreach($items as $item)
<x-layouts.sidebar.items :item="$item"></x-layouts.sidebar.items>
#endforeach
</ul>
</nav>
sidebar.items.blade.php
<li>
#if(count($item) > 0)
<a href="{{ $item['href'] }}" class="focus:text-blue-500 dark-focus:text-blue-400 focus:outline-none w-full transition duration-500 ease-in-out pl-4 py-2 text-gray-700 dark:text-gray-400 hover:bg-blue-200 dark-hover:bg-blue-500 transition duration-500 ease-in-out block">
{{ $item['text'] }} {{ $depth }}
</a>
#if (count($item['children']) > 0)
<ul class="pl-4">
#foreach($item['children'] as $child)
<x-layouts.sidebar.items :item="$child" :depth="$loop->parent->index"></x-layouts.sidebar.items>
#endforeach
</ul>
#endif
#else
<hr>
#endif
</li>
Sidebar\Items.php
<?php
namespace App\View\Components\Layouts\Sidebar;
use Illuminate\View\Component;
use Illuminate\View\View;
class Items extends Component
{
/**
* #var array
*/
public $item;
/**
* #var int
*/
public $depth;
/**
* Create a new component instance.
*
* #param array $item
* #param int $depth
*/
public function __construct($item = [], $depth = 1)
{
$this->item = $item;
$this->depth += $depth;
}
/**
* Get the view / contents that represent the component.
*
* #return View|string
*/
public function render()
{
return view('components.layouts.sidebar.items');
}
}
Data:
$this->items = [
[ 'href' => '/home', 'text' => 'Home', 'children' => [], 'active' => 'border-l-2 border-blue-500' ],
[ 'href' => 'javascript:void(0)', 'text' => 'Test', 'children' => [], 'active' => '' ],
[ 'href' => 'javascript:void(0)', 'text' => 'Websites', 'children' => [], 'active' => '' ],
[ 'href' => 'javascript:void(0)', 'text' => 'Websites', 'children' => [], 'active' => '' ],
[],
[
'href' => '/administration',
'text' => 'Administration',
'children' => [
[
'href' => 'javascript:void(0)',
'text' => 'Locked Devsites',
'active' => '',
'children' => []
]
],
'active' => ''
],
[ 'href' => 'javascript:void(0)', 'text' => 'Documentation', 'children' => [], 'active' => '' ],
[ 'href' => 'javascript:void(0)', 'text' => 'Logout', 'children' => [], 'active' => '' ]
];
Result:
Home 1
Test 1
Websites 1
Websites 1
Administration 1
Locked Devsites 5
Documentation 1
Logout 1

If I get it right what you want to achieve, you don't even need the $depth property, since the blade's #foreach directive has its own depth property which tells you how nested you are.
In your code, instead using:
:depth="$depth"
use:
:depth="$loop->depth"

Related

How to retrieve value from the migrations and display them

I used this for my migrations.
<?php
use Anomaly\Streams\Platform\Database\Migration\Migration;
class SnipcartModuleProductsCreateProductsFields extends Migration
{
/**
* The addon fields.
*
* #var array
*/
protected $fields = [
'name' => 'anomaly.field_type.text',
'description' => 'anomaly.field_type.text',
'sku' => [
'type' => 'anomaly.field_type.slug',
'config' => [
'slugify' => 'name',
'type' => '-'
],
],
'price' => [
"type" => "anomaly.field_type.decimal",
"config" => [
"min" => 0
]
],
'image' => 'anomaly.field_type.file',
'tags' => [
'type' => 'anomaly.field_type.tags',
'config' => [
'free_input' => true
]
]
];
}
Routing it from here
web.php
Route::get('/products', function () {
return view('products','ProductsController#getProducts');
});
Now How would I retrieve those values from here
ProductControllers.php
//name,description and price all the values should be pass to the products.blade.php
public function getProducts(){
$products = [];
return view('products', ['products' => $products]);
}
Then display it here
products.blade.php
#if (products)
<div class="row">
#foreach ($products as $product)
<div class="col-sm-4">
<div class="card" style="margin-bottom: 2rem;">
<div class="card-body">
<h4 class="card-title">{{ $product->name }}</h4>
<img />
<p class="card-text">{{ $product->description }}</p>
Buy for {{ $product->price }}
</div>
</div>
</div>
#endforeach
</div>
#endif

Twill CMS (Laravel): How to set up Repeater blocks? Repeaters display at wrong place

I wanted to use the repeater blocks of twill for my new project, set it up like in the documentation but it displays outside the section area just right beneath it in the body. Can´t edit it in the cms. How do I declare the right variable in the component to link to the repeater items? Or is there something else I forgot? Couldn´t find anything specific about my issue.
Checked everything multiple times, and tried different syntax and different input types
../site/blocks/competences.blade.php
#php
/** #var A17\Twill\Models\Behaviors\HasMedias $block */
#endphp
<div class="competences">
<div class="l-container">
<div class="competences-wrapper">
<h1 class="competences-headline">{{ $block->translatedInput('headline') }}</h1>
{!! $block->input('items') !!}
</div>
</div>
</div>
../admin/blocks/competences.blade.php
#formField('input', [
'name' => 'headline',
'label' => 'Headline',
'required' => true,
'translated' => true
])
#formField('repeater', [
'name' => 'items',
'type' => 'competences_item'
])
twill.php
<?php
return [
'block_editor' => [
'blocks' => [
'competences' => [
'title' => 'Competences',
'icon' => 'text',
'component' => 'a17-block-competences'
]
],
'repeaters' => [
'competences_item' => [
'title' => 'Competences Item',
'trigger' => 'Add competence',
'component' => 'a17-block-competences_item',
'min' => 3,
'max' => 3
]
],
]
];
should display inside the section/div, but displays beneath it just inside the body with of course no CSS styles
Fixed it this way:
#php
/** #var A17\Twill\Models\Behaviors\HasMedias $block */
#endphp
<div class="competences">
<div class="l-container">
<div class="competences-wrapper">
<h1 class="competences-headline">{{ $block->translatedInput('headline') }}</h1>
#foreach ($block->children as $child)
#include('site.blocks.competences_item', ['child', $child])
#endforeach
</div>
</div>
</div>

How to use buttonOptions on GridView on Yii2

I would like to change the default button name on grid view On YII2
on Yii 1 we have this:
http://www.yiiframew...s-in-cgridview/
array
(
'class'=>'CButtonColumn',
'template'=>'{email}{down}{delete}',
'buttons'=>array
(
'email' => array
(
'label'=>'Send an e-mail to this user',
'imageUrl'=>Yii::app()->request->baseUrl.'/images/email.png',
'url'=>'Yii::app()->createUrl("users/email", array("id"=>$data->id))',
),
'down' => array
(
'label'=>'[-]',
'url'=>'"#"',
'visible'=>'$data->score > 0',
'click'=>'function(){alert("Going down!");}',
),
),
),
I would like something like that for Yii2
For now I would like to change only the label.
Reading the documentation for Yii2 I tried that:
[
'class' => 'yii\grid\ActionColumn',
'buttonOptions' => [
[
'name' => 'update',
'additionalOptions' => [
'label' => 'Super Update',
]
],
[
'name' => 'delete',
'additionalOptions' => [
'label' => 'Super Delete',
]
],
],
],
But it does not work.
I know I can recreate the button from scratch with :
'buttons' => [
'update' => function ($url, $model) {
$t = 'index.php?r=site/update&id='.$model->id;
return Html::button('<span class="glyphicon glyphicon-pencil"></span>', ['value'=>Url::to($t), 'class' => 'btn btn-default btn-xs']);
},
],
But I do not want to do that.
thanks
buttonOptions will be applied to all default buttons, you can't separate them, but it's possible to apply general options (to all buttons):
'class' => 'yii\grid\ActionColumn',
'buttonOptions' => [
'title' => 'This is custom title for default 3 buttons',
],
If you want to use custom HTML options, you'll have to create new class, extend ActionColumn and overwrite (2) protected methods, for example:
<?php
namespace app\models;
use yii\grid\ActionColumn;
use yii\helpers\Html;
use Yii;
class customActionColumn extends ActionColumn
{
/**
* Initializes the default button rendering callbacks.
*/
protected function initDefaultButtons()
{
$this->initDefaultButton('view', 'eye-open', [
'title' => 'Super View',
]);
$this->initDefaultButton('update', 'pencil', [
'title' => 'Super Update',
]);
$this->initDefaultButton('delete', 'trash', [
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
'data-method' => 'post',
'title' => 'Super Delete'
]);
}
/**
* Initializes the default button rendering callback for single button
* #param string $name Button name as it's written in template
* #param string $iconName The part of Bootstrap glyphicon class that makes it unique
* #param array $additionalOptions Array of additional options
* #since 2.0.11
*/
protected function initDefaultButton($name, $iconName, $additionalOptions = [])
{
if (!isset($this->buttons[$name]) && strpos($this->template, '{' . $name . '}') !== false) {
$this->buttons[$name] = function ($url, $model, $key) use ($name, $iconName, $additionalOptions) {
$title = Yii::t('yii', ucfirst($name));
$options = array_merge([
'title' => $title,
'aria-label' => $title,
'data-pjax' => '0',
'title' => 'atata'
], $additionalOptions, $this->buttonOptions);
$icon = Html::tag('span', '', ['class' => "glyphicon glyphicon-$iconName"]);
return Html::a($icon, $url, $options);
};
}
}
}
Now in GridView you just need to specify custom class and that's all.
[
'class' => app\models\customActionColumn::className(),
],

Retrieve Data From Php Function

How to get data from php function? I want to call icon with:
<i class="<?php $this->Html->_($value['icon']);?>"></i>
Not work.
If it works would be like this
<i class="fa fa-cloud"></i>
But if call another function like:
<?php $this->Html->_($value['name']); ?>
Work well.
Main php function:
/**
* Retrieves the primary navigation for the client interface
*
* #param string $base_uri The base_uri for the currently logged in user
* #return array An array of main navigation elements in key/value pairs
* where each key is the URI and each value is an array representing that element including:
* - name The name of the link
* - active True if the element is active
* - sub An array of subnav elements (optional) following the same indexes as above
*/
public function getPrimaryClient($base_uri)
{
$nav = [
$base_uri => [
'name' => $this->_('Navigation.getprimaryclient.nav_dashboard'),
'active' => false
],
$base_uri . 'accounts/' => [
'name' => $this->_('Navigation.getprimaryclient.nav_paymentaccounts'),
'active' => false,
'secondary' => [
$base_uri . 'accounts/' => [
'name' => $this->_('Navigation.getprimaryclient.nav_paymentaccounts'),
'active' => false,
'icon' => 'fa fa-list'
],
$base_uri . 'accounts/add/' => [
'name' => $this->_('Navigation.getprimaryclient.nav_paymentaccounts_add'),
'active' => false,
'icon' => 'fa fa-plus-square'
],
$base_uri => [
'name' => $this->_('Navigation.getprimaryclient.nav_return'),
'active' => false,
'icon' => 'fa fa-arrow-left'
]
]
],
$base_uri . 'contacts/' => [
'name' => $this->_('Navigation.getprimaryclient.nav_contacts'),
'active' => false,
'secondary' => [
$base_uri . 'contacts/' => [
'name' => $this->_('Navigation.getprimaryclient.nav_contacts'),
'active' => false,
'icon' => 'fa fa-list'
],
$base_uri . 'contacts/add/' => [
'name' => $this->_('Navigation.getprimaryclient.nav_contacts_add'),
'active' => false,
'icon' => 'fa fa-plus-square'
],
$base_uri => [
'name' => $this->_('Navigation.getprimaryclient.nav_return'),
'active' => false,
'icon' => 'fa fa-arrow-left'
]
]
]
];
// Include the primary client's plugin navigation
return $this->getPluginNavPrimaryClient($nav, $base_uri);
}
I want to call icon here, as I mark // Get Icon
<?php
$active_nav = null;
?>
<ul class="nav navbar-nav">
<?php
foreach ($this->Html->ifSet($nav, array()) as $link => $value) {
$attributes = array();
$link_attributes = array();
$dropdown = !empty($value['sub']);
$active = false;
if ($value['active']) {
$active = true;
$attributes['class'][] = "active";
$active_nav = $value;
}
if ($dropdown) {
$attributes['class'][] = "dropdown";
$link_attributes['class'][] = "dropdown-toggle";
$link_attributes['data-toggle'][] = "dropdown";
// Set parent to active if child is
if (!$active) {
foreach ($this->Html->ifSet($value['sub'], array()) as $sub_link => $sub_value) {
if ($sub_value['active']) {
$attributes['class'][] = "active";
break;
}
}
}
}
?>
<li<?php echo $this->Html->buildAttributes($attributes);?>>
<a href="<?php $this->Html->_($link);?>"<?php echo $this->Html->buildAttributes($link_attributes);?>>
// Get Icon
<i class="<?php $this->Html->_($value['icon']);?>"></i>
// Get Icon
<?php
$this->Html->_($value['name']);
if ($dropdown) {
?>
<b class="caret"></b>
<?php
}
?>
</a>
<?php
if (!empty($value['sub'])) {
?>
<ul class="dropdown-menu">
<?php
foreach ($this->Html->ifSet($value['sub'], array()) as $sub_link => $sub_value) {
?>
<li>
<i class="<?php $this->Html->_($sub_value['icon']);?>"></i> <?php $this->Html->_($sub_value['name']);?>
</li>
<?php
}
?>
</ul>
<?php
}
?>
</li>
<?php
}
?>
</ul>
Did anyone have any ideas for this?
I really appreciate all the help.
Well, this is working b
<i class="<?php $this->Html->_($value['name']);?>"></i>
because there is name attribute is very first base URI
$base_uri => [
'name' => $this->_('Navigation.getprimaryclient.nav_dashboard'),
'active' => false
],
if you add icon as well, it will work
$base_uri => [
'name' => $this->_('Navigation.getprimaryclient.nav_dashboard'),
'active' => false,
'icon' => 'fa fa-cloud'
],
Hope this helps.

Not found error in Yii2

I am new to yii2 framework. I have created a controller,model and view. But i am getting not found error(404).
I have create the controller TestController.php, a view create.php and a model Test.php. I have try to run my project in particular controller that time im getting not found error message.
This is my controller page
The controller extends by Controller class. I've using alert boxes in controller page but i hasn't get in any alert.
class TestController extends Controller
{
namespace livefactory\modules\test\controllers;
public function actionCreate()
{
if(!Yii::$app->user->can('Test.Create')){
throw new NotFoundHttpException('You dont have permissions to view this page.');
}
$img = new ImageUpload();
$emailObj = new SendEmail;
$model = new Test;
return $this->render('create', [
'model' => $model,
]);
}
}
My model page
The model page defines all the variables used in the particular controller.
The table name is also defined.
namespace livefactory\models;
class Test extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'tbl_test';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['Name', 'Relationship', 'Age', 'Mail', 'Status'], 'required'],
[['Age'], 'integer'],
[['Name', 'Relationship', 'Mail', 'Status'], 'string', 'max' => 255]
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'Name' => Yii::t('app', 'Person Name'),
'Relationship' => Yii::t('app', 'Relationship'),
'Age' => Yii::t('app', 'Age'),
'Mail' => Yii::t('app', 'Mail'),
'Status' => Yii::t('app', 'Status'),
}
}
My view page
<?php
use yii\helpers\Html;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use kartik\widgets\ActiveForm;
use kartik\builder\Form;
use livefactory\models\Country;
use livefactory\models\State;
use livefactory\models\City;
use kartik\widgets\DepDrop;
use kartik\datecontrol\DateControl;
use livefactory\models\CustomerType;
/**
* #var yii\web\View $this
* #var common\models\Customer $model
*/
$this->title = Yii::t('app', 'Add Person', [
'modelClass' => 'Test',
]);
// $this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Customers'), 'url' => ['index']];
// $this->params['breadcrumbs'][] = $this->title;
?>
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5><?= Html::encode($this->title) ?> <small class="m-l-sm"><?php echo Yii::t ( 'app', 'Enter Person Details' ); ?></small></h5>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
</a>
<a class="close-link">
<i class="fa fa-times"></i>
</a>
</div>
</div>
<div class="ibox-content">
<div class="customer-form">
<?php
$form = ActiveForm::begin ( [
'type' => ActiveForm::TYPE_VERTICAL
] );?>
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title"><?php echo Yii::t ( 'app', 'Person Details' ); ?></h3>
</div>
<div class="panel-body">
<?php
echo Form::widget ( [
'model' => $model,
'form' => $form,
'columns' => 4,
'attributes' => [
'Person Name' => [
'type' => Form::INPUT_TEXT,
'options' => [
'placeholder' => Yii::t ( 'app', 'Enter Person Name' ).'...',
'maxlength' => 255
],
'columnOptions' => [
'colspan' => 3
]
]
,
'Relationship' => [
'type' => Form::INPUT_TEXT,
'options' => [
'placeholder' => Yii::t ( 'app', 'Enter Relationship' ).'...',
'maxlength' => 255
],
'columnOptions' => [
'colspan' => 3
]
]
]
]
);
echo Form::widget ( [
'model' => $model,
'form' => $form,
'columns' => 3,
'attributes' => [
'Age' => [
'type' => Form::INPUT_TEXT,
'options' => [
'placeholder' => Yii::t ( 'app', 'Enter Age' ).'...',
'maxlength' => 255
]
],
'Mail' => [
'type' => Form::INPUT_TEXT,
'options' => [
'placeholder' => Yii::t ( 'app', 'Enter Mail' ).'...',
'maxlength' => 255
]
],
'Status' => [
'type' => Form::INPUT_TEXT,
'options' => [
'placeholder' => Yii::t ( 'app', 'Enter Status' ).'...',
'maxlength' => 255
]
]
]
] );
?>
</div></div>
<?php
// echo Html::submitButton ( $model->isNewRecord ? Yii::t ( 'app', 'Create' ) : Yii::t ( 'app', 'Update' ), [
// 'class' => $model->isNewRecord ? 'btn btn-success customer_submit' : 'btn btn-primary customer_submit'
// ] );
ActiveForm::end ();
?>
</div>
</div>
</div>

Categories