Laravel backpack select_from_array - php

I am totally confused with select_from_array field in laravel backpack.
in my controller i am using a select_from_array field where in options i call a function,but when i run the code error is displayed. please help me with this.
Error : FatalErrorException in EventController.php line 106: syntax error, unexpected '$this' (T_VARIABLE)
controller.php
public $crud = array(
"model" => "App\Larapen\Models\Event",
"entity_name" => "event",
"entity_name_plural" => "events",
"route" => "admin/event",
"reorder" => true,
"reorder_label" => "name",
"reorder_max_level" => 2,
"details_row" => true,
// *****
// COLUMNS
// *****
"columns" => [
[
'name' => "id",
'label' => "ID"
],
],
"fields" => [
[
'name' => "event_name",
'label' => "Event name",
'type' => "text",
'placeholder' => "Event Name",
],
[
'name' => "event_topic",
'label' => "Event Topic",
'type' => "text",
'placeholder' => "Event Topic",
],
[
'name' => "event_type_id",
'label' => "Event Type",
'model' => "App\Larapen\Models\EventType",
'entity' => "eventType",
'attribute' => "name",
'type' => "select",
],
[
'name' => "about_event",
'label' => "About event",
'type' => "ckeditor",
'placeholder' => "About the Event",
],
[
'name' => "country_code",
'label' => "Country",
'type' => 'select_from_array',
'options' => $this->countries(),
'allows_null' => false,
],
],
);
public function countries()
{
..................
}
Please help me with this , why this happens? how to solve this issue?
Waiting for a response................

You can not use the pseudo-variable $this out of class method.
http://php.net/manual/en/language.oop5.properties.php
The pseudo-variable $this is available inside any class method when that method is called from within an object context. $this is a reference to the calling object
So if you want to set the attribute of crud's with $this, you can set it in the __construct function
public function __construct()
{
$this->crud['fields'][4] = $this->countries();
}
Or initialize it the __construct function
public $crud;
public function __construct()
{
$this->crud = array(
'model' => 'App\Larapen\Models\Event',
'entity_name' => 'event',
'entity_name_plural' => 'events',
'route' => 'admin/event',
'reorder' => true,
'reorder_label' => 'name',
'reorder_max_level' => 2,
'details_row' => true,
// *****
// COLUMNS
// *****
'columns' => [
[
'name' => 'id',
'label' => 'ID'
],
],
'fields' => [
[
'name' => 'event_name',
'label' => 'Event name',
'type' => 'text',
'placeholder' => 'Event Name',
],
[
'name' => 'event_topic',
'label' => 'Event Topic',
'type' => 'text',
'placeholder' => 'Event Topic',
],
[
'name' => 'event_type_id',
'label' => 'Event Type',
'model' => 'App\Larapen\Models\EventType',
'entity' => 'eventType',
'attribute' => 'name',
'type' => 'select',
],
[
'name' => 'about_event',
'label' => 'About event',
'type' => 'ckeditor',
'placeholder' => 'About the Event',
],
[
'name' => 'country_code',
'label' => 'Country',
'type' => 'select_from_array',
'options' => $this->countries(),
'allows_null' => false,
],
],
);
}

Related

Extend an assiociative Array with variable

can someone help me, how I can extend an assiociative array with a variable?
I have a loop (foreach):
foreach($this->getWarehouseListForm() as $wareHouse) {
$wareHouseList[] =
[
"title" => "wareHouse[105]",
"form" => [
"storeId[105]" => [
"type" => "inputText",
"options" => [
"name" => "TSL",
],
],
],
];
}
And I want to extend a object like this:
"sections" => [
[
"title" => 'Schuhe24Assistant.ftpServerTitle',
"description" => 'Schuhe24Assistant.ftpServerDescription',
"form" => [
"ftpServer" => [
'type' => 'text',
'defaultValue' => 'ftp.hisasp2.com',
'options' => [
'name' => 'Schuhe24Assistant.ftpServer',
'required' => true,
]
],
"ftpUser" => [
'type' => 'text',
'defaultValue' => 'username',
'options' => [
'name' => 'Schuhe24Assistant.ftpUser',
'required' => true,
]
],
"ftpPassword" => [
'type' => 'text',
'defaultValue' => 'password',
'options' => [
'name' => 'Schuhe24Assistant.ftpPassword',
#'isPassword' => true,
'required' => true,
],
],
"deleteFiles" => [
'type' => 'toggle',
'defaultValue' => true,
'options' => [
'name' => 'Schuhe24Assistant.deleteFiles',
],
],
],
],
], $wareHouseList
But this code produces nothing (no error output) and the structure check fails in this case. If I remove the variable, the structure check is OK.
Can someone help me out?
Kind regards
Henning
Push your $wareHouseList array into the $sections array in the following ways
$sections['wareHouseList'] = $wareHouseList
It should be working.

yajrabox datatable translation

I'm using yajrabox datatable and I'm having this problem when I'm trying to use localization on titles.
protected $exportColumns = [
'name' => ['data' => 'name', 'name' => 'name', 'title' => trans('datatable.classroomDatatable.classroom'), 'className' => "text-center", 'printable' => true, 'exportable' => true],
'course' => ['data' => 'course', 'name' => 'course', 'title' => trans('datatable.classroomDatatable.branch'), 'className' => "text-center", 'printable' => true, 'exportable' => true],
'caretaker' => ['data' => 'caretaker', 'name' => 'caretaker', 'title' => trans('datatable.classroomDatatable.caretaker'), 'className' => "text-center", 'printable' => true, 'exportable' => true, ]
];
Error:
Constant expression contains invalid operations
What I'm doing wrong on this?
Thanks in advance

Populate Multi select drop drop down zend 3

Hello everyone i am getting really hard time populating my multi select drop down in my form.
what i have tried so far was adding factory for my form which is like this
class MovieFormFactory
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$entityManager = $container->get('doctrine.entitymanager.orm_default');
$actors = $entityManager->getRepository(Actor::class)->findAll();
$form = new MovieForm();
$form->setActors($data);
return $form;
}
}
my form
Class MovieForm extends Form
{
private $actors = [];
public function setActors($actorsData){
$this->actors = $actors
}
public function __construct()
{
parent::__construct('post-form');
$this->setAttribute('method', 'post');
$this->addElements();
$this->addInputFilter();
$this->add([
'type' => 'select',
'name' => 'actors',
'attributes' => [
'id' => 'actors',
'multiple' => true
],
'options' => [
'label' => 'Actors',
'value_options' => $this->actors,
],
]);
$this->add([
'type' => 'select',
'name' => 'directors',
'attributes' => [
'id' => 'directors',
],
'options' => [
'label' => 'Director',
'value_options' => $this->getOptionForSelect($directors),
],
]);
}
/**
* This method adds elements to form (input fields and submit button).
*/
protected function addElements()
{
// Add "title" field
$this->add([
'type' => 'text',
'name' => 'title',
'attributes' => [
'id' => 'title'
],
'options' => [
'label' => 'Title',
],
]);
// Add "description" field
$this->add([
'type' => 'textarea',
'name' => 'description',
'attributes' => [
'id' => 'description'
],
'options' => [
'label' => 'Description',
],
]);
// Add "tags" field
// $this->add([
// 'type' => 'text',
// 'name' => 'actors',
// 'attributes' => [
// 'id' => 'actors',
// 'multiple' => 'multiple'
// ],
// 'options' => [
// 'label' => 'Actors',
// 'value_options' => $this->getOptionForSelect(),
// ],
// ]);
// Add "status" field
$this->add([
'type' => 'select',
'name' => 'status',
'attributes' => [
'id' => 'status'
],
'options' => [
'label' => 'Status',
'value_options' => [
MovieStatusEnum::STATUS_DRAFT => MovieStatusEnum::STATUS_DRAFT,
MovieStatusEnum::STATUS_PUBLISHED => MovieStatusEnum::STATUS_PUBLISHED,
]
],
]);
// Add the submit button
$this->add([
'type' => 'submit',
'name' => 'submit',
'attributes' => [
'value' => 'Create',
'id' => 'submitbutton',
],
]);
}
/**
* This method creates input filter (used for form filtering/validation).
*/
private function addInputFilter()
{
$inputFilter = new InputFilter();
$this->setInputFilter($inputFilter);
$inputFilter->add([
'name' => 'title',
'required' => true,
'filters' => [
['name' => 'StringTrim'],
['name' => 'StripTags'],
['name' => 'StripNewlines'],
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 1,
'max' => 1024
],
],
],
]);
$inputFilter->add([
'name' => 'description',
'required' => true,
'filters' => [
['name' => 'StripTags'],
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 1,
'max' => 4096
],
],
],
]);
$inputFilter->add([
'name' => 'actors',
'required' => true,
]);
}
private function getOptionForSelect($data)
{
foreach ($data as $person) {
$selectData[$person->getId()] = $person->getName();
}
return $selectData;
}
}
and this is my registered factory in module.config.php
'form_elements' => [
'factories' => [
Form\MovieForm::class => Form\Factory\MovieFormFactory::class
]
],
but nothing seems to work i am unable to show my actors while creating a movie and unable to show selected actors while editing a movie can some please guide me here i am new to zend.
In the constructor, the line value_options' => $this->actors is wrong because $this->actors is not set yet. In your factory you write :
$form = new MovieForm();
$form->setActors($data);
You must therefore declare the public method setActors() in the class MovieForm which will take care of setting up the options array.
public function setActors($array)
{
$this->get('actors')->setValueOptions($array);
}

How to add a custom field to orders in Shopware 6?

By default you can add custom fields to several entities, however I don’t see the order entity in the list of available entities.
Is it possible to add such a field for order so user can fill it in the checkout process, right before sending the order?
And is it possible to add a field for the order and for each order item individually?
Here is one example on how to add to add custom fields to order entity:
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->upsert([
[
'name' => self::FIELD_NAME,
// 'global' => true,
'config' => [
'label' => [
'de-DE' => 'Name',
'en-GB' => 'Name'
]
],
'customFields' => [
[
'name' => 'name',
'type' => CustomFieldTypes::DATETIME,
'config' => [
'type' => 'date',
'dateType' => 'date',
'label' => [
'de-DE' => 'Date',
'en-GB' => 'Date'
]
]
],
[
'name' => 'name',
'label' => "Time",
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'de-DE' => 'name',
'en-GB' => 'name'
]
]
],
[
'name' => 'name',
'label' => "name",
'type' => CustomFieldTypes::INT,
'config' => [
'label' => [
'de-DE' => 'name',
'en-GB' => 'name'
]
]
]
],
'relations' => [[
'entityName' => 'order'
]],
]
], $context);

Adding a conditional statement to an array of fields?

I need to include a conditional statement into the following array called $fields.
$fields = [
'program_id' => [
'type' => 'select',
'label' => 'Program',
'opts' => ["One", "Two", "Three"],
],
'name' => [
'label' => 'Job Name'
],
'start_date' => [
'class' => 'date-picker',
'label' => 'Job Starts' . $req,
'val' => $job->start_date ? dateToPicker($job->start_date) : null
],
'end_date' => [
'class' => 'date-picker',
'label' => 'Job Ends' . $req,
'val' => $job->end_date ? dateToPicker($job->end_date) : null
],
'section' => [
'type' => 'hidden',
'val' => 'details'
],
];
if (!$job->id && $program)
{
$fields['job_copy'] = [
'label' => 'Copy Job From',
'type' => 'select',
'textAsValue' => false,
'_comment' => 'Selecting a job here will copy all job information except the name.',
'opts' => array_replace([0 => '-- Select Job --'], $program->jobs()->lists('name', 'id')->all())
];
}
$fields[] = [
'type' => 'submit',
'label' => "Save",
'class' => 'btn btn-primary !important'
];
}
I need to move the conditional statement to the top so that it is the first thing displayed on the form. However, when I move it to the top it disappears. How can I integrate the conditional check into the top of the form as opposed to at the bottom where it currently displays?
$fields = [];
if (!$job->id && $program)
{
$fields['job_copy'] = [
'label' => 'Copy Job From',
'type' => 'select',
'textAsValue' => false,
'_comment' => 'Selecting a job here will copy all job information except the name.',
'opts' => array_replace([0 => '-- Select Job --'], $program->jobs()->lists('name', 'id')->all())
];
}
$fields2 = [
'program_id' => [
'type' => 'select',
'label' => 'Program',
'opts' => ["One", "Two", "Three"],
],
'name' => [
'label' => 'Job Name'
],
'start_date' => [
'class' => 'date-picker',
'label' => 'Job Starts' . $req,
'val' => $job->start_date ? dateToPicker($job->start_date) : null
],
'end_date' => [
'class' => 'date-picker',
'label' => 'Job Ends' . $req,
'val' => $job->end_date ? dateToPicker($job->end_date) : null
],
'section' => [
'type' => 'hidden',
'val' => 'details'
],
];
$fields = array_merge($fields,$fields2);
$fields[] = [
'type' => 'submit',
'label' => "Save",
'class' => 'btn btn-primary !important'
];

Categories