Custom widget in Elementor with the same controls - elementor

I am creating a custom Elementor widget with two image controls, unfortunately, I can only get one of the two to work in the content tab. I thought if I add two sections it would work but seems not, looked at Elementor documentation at https://developers.elementor.com/docs/editor-controls/control-media/ but cant find anything there.
$this->start_controls_section(
'section_image_one',
[
'label' => esc_html__( 'Image One' , $this->domain ),
]
);
$this->add_control(
'image',
[
'label' => esc_html__( 'Choose Image', $this->domain ),
'type' => Controls_Manager::MEDIA,
'dynamic' => [
'active' => true,
],
'default' => [
'url' => Utils::get_placeholder_image_src(),
],
]
);
$this->end_controls_section();
$this->start_controls_section(
'section_image_two',
[
'label' => esc_html__( 'Image Two' , $this->domain ),
]
);
$this->add_control(
'image',
[
'label' => esc_html__( 'Choose Image', $this->domain ),
'type' => Controls_Manager::MEDIA,
'dynamic' => [
'active' => true,
],
'default' => [
'url' => Utils::get_placeholder_image_src(),
],
]
);
$this->end_controls_section();

It looks like it is a rather simple solution
.....
$this->add_control(
'image',
.....
As mentioned on https://developers.elementor.com/docs/editor-controls/regular-control/, the "image" mentioned is a control name and needs to be unique.

Related

Cakephp4 save with associations with more than one level

I'm developing with CakePHP 4 and try to save a nested entity.
The new data are looking like that:
$data = [
'device_status_id' => '3',
'name' => 'myDevice',
'modules' => [
[
'name' => 'Autodetect',
'module_state_id' => '1',
'module_class_id' => '12',
'module_type_id' => '4',
'ports' => [
[
'physical_port' => '1',
'port_unit_id' => '1',
'port_identity' => '201901',
'name' => 'Analog-1',
],
[
'physical_port' => '2',
'port_unit_id' => '1',
'port_identity' => '201902',
'name' => 'Analog-2',
],
],
],
]
];
The structure is looking like that: $device -> hasMany($modules) -> hasMany($ports)
The table associations with hasMany are also given and working:
e.g.
$this->hasMany('Ports', [
'foreignKey' => 'module_id',
'dependent' => true,
'cascadeCallbacks' => true,
]);
The same for device as well...
In my controller I'm doing the following:
$device = $this->Devices->newEmptyEntity();
$device->setDirty('modules', true);
$device->setDirty('ports', true); // right?
// adding more info to $device
if ($this->request->is('post')) {
$device = $this->Devices->patchEntity($device, $this->request->getData(), [
'validate' => false,
'associated' => [
'DeviceTypes',
'DeviceStatuses',
'Modules',
'Modules.ModuleStates',
'Modules.ModuleClasses',
'Modules.ModuleTypes',
'Modules.Ports',
]
]);
$this->Devices->save($device)
Now the save fails, as the validator is complaining about a missing port->id.
So what is wrong here? The modules are created in the DB, but not the associated ports:
[
'device' => [
'modules' => [
(int) 0 => [
'ports' => [
(int) 0 => [
'id' => [
'_required' => 'This field is required',
],
....
Many thanks for your hints how I can force the ports to be added to the DB.

saving model with deep association belonging to both higher models

I have three tables, Articles, Comments, and Tags.
Tags belong to both Articles and Comments.
$this->Articles->patchEntity($entity, $this->request->getData(), [
'associated' => ['Comments.Tags']
]);
with the following error:
SQLSTATE[HY000]: General error: 1364 Field 'article_id' doesn't have a default value
Please try correcting the issue for the following table aliases:
CommentsArticles
but if I save with only 'associated' => ['Comments'] it works saving the Article and Comments with join table associations, just doesn't save any Tags.
Articles table has these associations:
$this->hasMany('Tags', [
'foreignKey' => 'article_id'
]);
$this->belongsToMany('Comments', [
'foreignKey' => 'article_id',
'targetForeignKey' => 'comment_id',
'joinTable' => 'comments_articles'
]);
Comments table has these associations:
$this->hasMany('Tags', [
'foreignKey' => 'comment_id'
]);
$this->belongsToMany('Articles', [
'foreignKey' => 'comment_id',
'targetForeignKey' => 'article_id',
'joinTable' => 'comments_articles'
]);
and Tags table has these associations:
$this->belongsTo('Comments', [
'foreignKey' => 'comment_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Articles', [
'foreignKey' => 'article_id',
'joinType' => 'INNER'
]);
This is the entity after patching looks like this.
object(App\Model\Entity\Article) {
'title' => 'example article name',
'users' => [
'_ids' => []
],
'comments' => [
(int) 0 => object(App\Model\Entity\Comment) {
'id' => (int) 1,
'content' => 'this is a comment',
'tags' => [
(int) 0 => object(App\Model\Entity\Tag) {
'name' => 'example tag name',
'[new]' => true,
'[accessible]' => [
'comment_id' => true,
'article_id' => true,
'comment' => true,
'article' => true
],
'[dirty]' => [
'name' => true
],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Tags'
}
],
'[new]' => false,
'[accessible]' => [
'content' => true,
'tags' => true,
'articles' => true
],
'[dirty]' => [
'tags' => true
],
'[original]' => [
'tags' => [
(int) 0 => [
'name' => '0'
]
]
],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Comments'
}
],
'[new]' => true,
'[accessible]' => [
'title' => true,
'tags' => true,
'comments' => true,
'users' => true
],
'[dirty]' => [
'title' => true,
'users' => true,
'comments' => true
],
'[original]' => [
'users' => []
],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Articles'
}
CaekPHP doesn't support that, it can only populate foreign keys of direct associations / in one direction. You could for example:
prepopulate the foreign key fields (which will of course only work when the article and/or comment already exists)
manually save the tags separately using the primary keys of the article and comment records
create association classes that pass the article primary key into the options when saving the article, and uses that to populate the article_id field when saving the tag
hook into the saving process on table level to pass on the article primary key and populate the tags with it
Here's a quick and dirty example for the latter solution, which should also give you an idea on how it could work on association level:
In ArticlesTable:
public function beforeSave(
\Cake\Event\Event $event,
\Cake\Datasource\EntityInterface $entity,
\ArrayObject $options
) {
if (isset($options['Articles.id'])) {
unset($options['Articles.id']);
}
}
protected function _onSaveSuccess($entity, $options)
{
if ($options['_primary']) {
$options['Articles.id'] = $entity->get('id');
}
return parent::_onSaveSuccess($entity, $options);
}
In TagsTable:
public function beforeSave(
\Cake\Event\Event $event,
\Cake\Datasource\EntityInterface $entity,
\ArrayObject $options
) {
if (!$options['_primary'] &&
isset($options['Articles.id'])
) {
$entity->set('article_id', $options['Articles.id']);
}
}

How to display array data in Yii2 GridView using ActiveDataprovider?

In my db,
"email" : [
"amnop#mailinator.com",
"abc#mail.com"
],
When I print_r($model->email),
it shows
Array ( [0] => amnop#mailinator.com [1] => abc#mail.com )
In my GridView,
<?= GridView::widget([
'dataProvider' => $dataProvider,
----
'columns' => [
-----
'price',
[
'attribute' => 'email',
'value' => function($model) {
//I need help here... I prefer any foreach function
}
],
-----
]
?>
I have to display all the emails in the same column. How to do this?
Edit
I use ActiveDataprovider as I'm getting the values from my db.
Depending on what you want to achieve, you can just implode emails array:
[
'attribute' => 'email',
'value' => function($model) {
if (is_array($model->email)) {
return implode(', ', $model->email);
}
return $model->email;
}
],
assuming you an array as
$data = [
['email' => 'amnop#mailinator.com'],
['email' => 'abc#mail.com'],
...
['email' => 'youremail100#mail.com'],
];
you can use an ArrayDataProvider
$provider = new ArrayDataProvider([
'allModels' => $data,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'attributes' => [ 'email'],
],
]);
send the data provider to the as usual
so in gridview you can use ,
<?= GridView::widget([
'dataProvider' => $dataProvider,
----
'columns' => [
-----
'price',
[
'attribute' => 'email',
'value' => function($model) {
//I need help here...
}
],
-----
]
?>
you can take a look at yii2 guide https://www.yiiframework.com/doc/guide/2.0/en/output-data-providers
and doc https://www.yiiframework.com/doc/api/2.0/yii-data-arraydataprovider

How to add ng-model to Symfony form type checkboxes

I have a few checkboxes in a symfony form like
->add('countries', EntityType::class, [
'class' => 'Catalog:Countries',
'choice_label' => 'name',
'multiple' => true,
'expanded' => true
])
how can I add ng-model to each checkbox to bind them to my angularJS script?
You're looking for the attr option for the parent <select> element or choice_attr option for the child <option> elements.
Example:
$builder->add('countries', EntityType::class, [
'class' => 'Catalog:Countries',
'choice_label' => 'name',
'multiple' => true,
'expanded' => true,
'attr' => [
'[ng-model]' => 'countries'
],
'choice_attr' => [
'[selected]' => 'isCountrySelected()'
]
]);
I found the solution here
choice_attr
however, if some of the checkboxes are selected then you will loose this information on page load

How can I show attribute value with check Box in Yii 2 GridView

i like to put mission_id in checkbox
$req = 'select c.mission_id as mission_id, c.user_id as user_id from order c';
$dataProvider = new SqlDataProvider([
'sql' => $req,
]);
return $this->render('factures', [
'dataProvider' => $dataProvider,
]);
in _index.php
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'mission_id',
'user_id',
],
]); ?>
This it's ok, but when i use checkbox, it s KO : Trying to get property of non-object
[
'attribute' => 'id',
'format' => 'raw',
'value' => function($data) {
return '<input type="checkbox" name="chk_group" value="'.$data->mission_id.'" />Mission : '.$data->mission_id;
},
],
Your help please
a some response : Trying to get property of non-object with :
[
'attribute' => 'id',
'format' => 'raw',
'value' => function($model) {
return '<input type="checkbox" name="chk_group" value="'.$model->mission_id.'" />Mission : '.$model->mission_id;
},
],

Resources