Hello :) I'm just learning Wordpress and I can not find an answer to a question about how to write code.
Here is my code:
if ( !function_exists( 'mythemename_social_sites_list' ) ) :
function mythemename_social_sites_list() {
return [
[ 'name' => 'Twitter', 'icon' => 'fab fa-twitter' ],
[ 'name' => 'Facebook', 'icon' => 'fab fa-facebook-f' ],
[ 'name' => 'Google Plus', 'icon' => 'fab fa-google-plus-g' ],
];
}
endif;
In general, the function returns many more elements of the array, but here I gave only 3 to be more visible. What's the problem?
Is there any sense to write it this way? In a sense, a function that returns an array but does nothing with it? From what I understand, functions should be used when they do something. For example, this one would take an array, then do something with it and return the changed one.
Would not it be better to just write this:
global $mythemename_social_sites_list;
$mythemename_social_sites_list = [
[ 'name' => 'Twitter', 'icon' => 'fab fa-twitter' ],
[ 'name' => 'Facebook', 'icon' => 'fab fa-facebook-f' ],
[ 'name' => 'Google Plus', 'icon' => 'fab fa-google-plus-g' ],
];
Global, because I use this array in several places.
Thanks in advance for the brightening :)
Related
I tried to send some closure to the livewire component inside a nested array but it produce this error:
Livewire encountered corrupt data when trying to hydrate the [data-table] component. Ensure that the [name, id, data] of the Livewire component wasn't tampered with between requests.**
What I sent to the component :
<livewire:data-table
:model="$modelClass"
:custom="[
[
'label' => 'E-Mail',
'column'=> 'email'
],
]"
:exclude="['password', 'email_verified_at', 'remember_token', 'updated_at']"
:include="[
[
'label' => 'Role',
'column'=> 'role.name',
'format'=> fn($value, $row) => '<strong>'.ucwords($value).'</strong>',
'formatType' => 'html'
],
[
'label' => 'Search Engine',
'links' => [
[
'title' => fn($row) => $row->name.' Google',
'link' => fn($row) => 'https://google.com/search?q='.$row->name,
'type' => 'button',
]
],
],
[
'label' => 'Social Media',
'links' => [
[
'title' => 'Facebook',
'link' => 'https://facebook.com',
'type' => 'link'
],
[
'title' => 'Instagram',
'link' => 'https://instagram.com',
'type' => 'link'
]
]
],
[
'label' => 'Email Provider',
'links' => [
[
'title' => function($row){
if(str_contains($row->email, 'gmail')){
return 'Google';
}elseif(str_contains($row->email, 'yahoo')){
return 'Yahoo';
}else{
return 'Unidentified';
}
},
'class' => 'cursor-pointer',
'onclick' => 'return false',
]
]
]
]"
/>
Then i check in ComponentCheckshumManager class, my array that contains the closure become empty when go through Hashing using json_encode.
Before Hashing :
Data before hashing
After Hashing :
Data after hashing
This error happens only when refreshing the component, is there any correct way to send closure to the livewire component? Please help.
Depends on name. If it's an attribute, then issue is somewhere else, if it's a magic function, then you have a problem. Each time a template modifies original data, you will get corrupt data. Even if it's $user->full_name magic function, it will corrupt original data.
Hello.
I currently have a problem with the AWS Route-53 API. To create a record you need to call a function, which itself needs an array of inputs.
I want to create a record set here and for that I have some POST values. One of them, $_POST['record_value'], is a textarea and has multiple lines. I loop through them. This is to enable multiple values for one record. The code is as follows when you hardcode it as one value in ResourceRecords;
$result = $this->route53->changeResourceRecordSets([
'ChangeBatch' => [
'Changes' => [
[
'Action' => 'CREATE',
'ResourceRecordSet' => [
'Name' => $recordName,
'ResourceRecords' => [
[
'Value' => $recordValue
],
],
'TTL' => $recordTtl,
'Type' => $recordType,
],
],
],
'Comment' => 'Routing Record Set',
],
'HostedZoneId' => $this->zone,
]);
Hower. I want to make ResourceRecords dynamically. For every line in the textarea I need a new set of the following part of the code;
[
'Value' => $recordValue
],
What I thought is the following;
$newData = [];
foreach(explode("\r\n", $recordValue) as $valLine) {
$newData[] = ["Value" => $valLine];
}
$result = $this->route53->changeResourceRecordSets([
'ChangeBatch' => [
'Changes' => [
[
'Action' => 'CREATE',
'ResourceRecordSet' => [
'Name' => $recordName,
'ResourceRecords' => [
$newData
],
'TTL' => $recordTtl,
'Type' => $recordType,
],
],
],
'Comment' => 'Routing Record Set',
],
'HostedZoneId' => $this->zone,
]);
However, this seems to return an exception: Found 1 error while validating the input provided for the ChangeResourceRecordSets operation:↵[ChangeBatch][Changes][0][ResourceRecordSet][ResourceRecords][0] must be an associative array. Found array(1).
Am I building the array wrong or am I doing this wrong alltogether?
$newData is already an array, you don't need to wrap it in another array.
'ResourceRecords' => $newData,
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.
I am getting the error:
The input was not found in the haystack
After form post. Please see the selection tag code lines below:
// Add "roles" field
$this->add([
'type' => 'select',
'name' => 'roles',
'attributes' => [
'multiple' => 'multiple',
'options'=>$this->role_desc,
'inarrayvalidator' => false,
'class'=>'form-control'
],
'options' => [
'label' => 'Role(s)',
],
]);
// Add input for "roles" field
$inputFilter->add([
'class' => ArrayInput::class,
'name' => 'roles',
'required' => true,
'haystack'=>$this->role_ids,
'filters' => [
['name' => 'ToInt'],
],
'validators' => [
['name'=>'GreaterThan', 'options'=>['min'=>1]],
['name'=>'InArray', 'options'=>['haystack'=>$this-
>role_ids]]
],
]);
The InArray seems to be validating well, lm just no sure what is bringing up the exception. Thank you in advance.
Actually , your issue is similar to link
To solve this, change your validators definition to:
'validators' => [
['name'=>'GreaterThan', 'options'=>['min'=>1]],
[
'name' => 'Explode',
'options' => array(
'validator' => [
'name'=>'InArray',
'options'=> [
'haystack'=>$this->role_ids
]
]
)
]
],
Unfortunately, I do not think there is a "cleaner" way to do this.
Alternately, Maybe you could use the MultiCheckbox.
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']
]
]
]
];
}