Store / Website Column in Magento 2 Admin Grid - Custom Module - php

I have created a custom module in magento 2. I have two tables, first one is the main table and the other one is for the stores. The second table has the foreign key constraint of the first table's primary key and the store id column. I have successfully created the gird and the form. The only problem that I am having is that I am not able to load store ids in the grid. I am using ui_component approach, means, the grid is being rendered using xml.

Check Magento\Cms\Block\Adminhtml\Page\Grid
/**
* Check is single store mode
*/
if (!$this->_storeManager->isSingleStoreMode()) {
$this->addColumn(
'store_id',
[
'header' => __('Store View'),
'index' => 'store_id',
'type' => 'store',
'store_all' => true,
'store_view' => true,
'sortable' => false,
'filter_condition_callback' => [$this, '_filterStoreCondition']
]
);
}

Related

Make a select2_multiple sortable

How do we make a select2_multiple sortable? I have a users table and a badges table. Naturally, I want to have a user_badge pivot table which will keep track of all the badges a user has and also sort it based on the order that I define using the select2_multiple.
Currently, I have my setupCreateOperation() setup as follows:
protected function setupCreateOperation()
{
$this->crud->addField([
'name' => 'badges_multi',
'label' => 'Badges',
'type' => 'select2_multiple',
'attribute' => 'internal_name',
'entity' => 'badges_multi',
'model' => 'App\Models\Badges',
'pivot' => true,
'sortable' => true,
'sortable_options' => [
'handle' => '.my-custom-handle',
],
]);
}
This returns the select2_multiple but the input is not sortable, i.e. I can drag and drop to rearrange. It only returns badges in alphabetical order
The select_multiple field does not have the ordering functionality.
To order you can use the select_and_order field or the repeatable field
You can also create a custom version of the select_multiple field that has the order functionality if you want to implement it that way.
Doing php artisan backpack:field select_multiple_with_order --from=select_multiple would create you a new file in your resources folder that is equal to Backpack select_multiple, from there you can add the functionality you need, and re-use it in other cruds if you need it there too.
Wish you the best.

How to save multiple values from select2_from_ajax_multiple in Backpack laravel

everyone
I am tring to save the Multiple values from select option, but i don't have knowledge about how to save in laravel Backpack .I have been trying to solve the error from 2 days, but still it's not working from my side.
Please have a look, help would be appreciated.
Here is Screen shot of multiple select option ---
But it does not saving in db, but if i am going to edit the record , it gives this error ---
Here is my controller code
CRUD::addField([ // select2_from_ajax: 1-n relationship
'label' => "City Name", // Table column heading
'type' => 'select2_from_ajax_multiple',
'name' => 'location_id', // the column that contains the ID of that connected entity;
'entity' => 'location', // the method that defines the relationship in your Model
'attribute' => 'name', // foreign key attribute that is shown to user
'tab' => 'Texts',
'data_source' => url('api/location'), // url to controller search function (with /{id} should return model)
'placeholder' => 'Select location', // placeholder for the select
'include_all_form_fields' => true, //sends the other form fields along with the request so it can be filtered.
'minimum_input_length' => 0, // minimum characters to type before querying results
'dependencies' => ['state'], // when a dependency changes, this select2 is reset to null
// 'method' => 'GET', // optional - HTTP method to use for the AJAX call (GET, POST)
"allows_multiple" => true,
'pivot' => true,
]);

how to add country column in prestashop AdminCustomersControllerCore

i want to add an extra column in AdminCustomersControllerCore so that it will show a new column in my back office manage customer page.
Use the hook actionControllerNameListingFieldsModifier to add fields.
Every admin controller calls this hook before generating the list/table.
Hook::exec('action'.$this->controller_name.'ListingFieldsModifier', array(
'select' => &$this->_select,
'join' => &$this->_join,
'where' => &$this->_where,
'group_by' => &$this->_group,
'order_by' => &$this->_orderBy,
'order_way' => &$this->_orderWay,
'fields' => &$this->fields_list,
));
You will need to modify select and fields variable to include your own. You might need to modify join variable as well if you have data in a custom table.

Adding associated value by not using foreign key in CakePHP 2

Recently I've started using CakePHP by starting from using Bake to get simple CRUD.
I have one Model called "Contract" which is associated with "Product" and "Customer" as following.
public $belongsTo = array(
'Customer' => array(
'className' => 'Customer',
'foreignKey' => 'customer_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Product' => array(
'className' => 'Product',
'foreignKey' => 'product_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Now I wanted to customize the add view (add.ctp) created by Bake by not using select box as an input form for customer_id and product_id. Instead of using select box which shows all the possible name of product/customer that are associated with foreign key I want to use text field so that people can type in one of the field (for example product_code, customer_code) from Product / Customer table and convert it to foreign key to create a row for Contract table. In case there is a chance that no values be found from Customer / Product field I want to skip adding a row to Contract table.
I would like to know if this is possible.
Thank you,
The basic concept is this: Use a JS lib of your choice, guess it's jquery. Watch the text field for changes, on change make an ajax GET request with the value of the text field to a controller / action of your app that returns json. On click on one of the results set the id of the selected record to a hidden field. Done.
If you search Google or Stackoverflow you'll find plenty of examples.

Yii create CButton Column for field that exists in another model

I'm trying to add the following functionality, however I'm not sure where to start. Any advice, examples, or direction would be greatly appreciated.
I want to add button to the cgridview of the main model in this context. Each of the records available in the cgridview for this model, have a unique attribute called lot for example R3XSEF9
There is another secondary table/model in my database that has records with this same lot attribute. However, this table only has certain records out of all the possible records, sometimes duplicates, and has a set of different attributes.
What I'd like to do is, using the lot attribute, for example lot R3XSEF9 from my cgridview, search the secondary table to see if there is one ore more corresponding rows which contains that same lot R3XSEF9.
If so, I would like the button to be displayed in my CButtonColumn and link to the views for those corresponding models of the secondary table. If not, I would like no button to appear.
Thanks for any help. If any clarification is required, I would be happy to do so.
First of all you need to link tables using "relations" function in model class. If you use FOREIGN KEY constraint in DB relations already filled.
SQL statement:
CREATE TABLE Model1
(
...
FOREIGN KEY(lot) REFERENCES MainModel(lot) ON UPDATE CASCADE ON DELETE RESTRICT,
...
)
Model class:
class MainModel extends CActiveRecord
{
...
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'lots' => array(self::HAS_MANY, 'Model2', 'lot'),
);
}
Then you can use custom button column in your grid (view file) like this:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'main-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
...
array(
'class' => 'CButtonColumn',
'template' => '{lots}',
'header' => 'Lots',
'buttons' => array(
'lots' => array(
'label' => 'Lots',
'imageUrl' => Yii::app()->request->baseUrl.'/img/....png',
'url' => 'Yii::app()->createUrl("controller1/lotlistbymainid", array("id" => $data->id))',
'visible' => 'count($data->lots) > 0',
),
),
),
Explanation of button parameters to be passed thru "buttons" array you can find here. Especialy this part:
buttons property
public array $buttons;
the configuration for additional buttons. Each array element specifies a single button which has the following format:
'buttonID' => array(
'label'=>'...', // text label of the button
'url'=>'...', // a PHP expression for generating the URL of the button
'imageUrl'=>'...', // image URL of the button. If not set or false, a text link is used
'options'=>array(...), // HTML options for the button tag
'click'=>'...', // a JS function to be invoked when the button is clicked
'visible'=>'...', // a PHP expression for determining whether the button is visible
)

Categories