How to display a controller variable in view(blade) - laravel-blade

I have a variable generated by the controller in laravel 5.8, I want to display this variable on the view
I have tried using the conventional {!! $variable !!} but it returns
#section('page_heading',"System Dashboard | {!! $region !!}")
I expect to get Kalipso Dashboard |Northern Region but instead am getting Kalipso Dashboard |

It will be set as below
Suppose value of $region = 'Your custom string message here.';
#section('page_heading',"System Dashboard | {{ $region }}")

Found the solution.
Used #section('page_heading',"System Dashboard | $region ")

Related

Get custom post type label instead of name - WordPress + Timber

I created 3 custom post types in WordPress : dissertation, subject-imposed, subject-free
In each custom post type the user is allowed to create only ONE post.
I created a menu composed with those three custom post types which allow to switch from post to another from the same user.
But I got a problem. The menu item display the name of the post type and not the label.
I got : DISSERTATION / SUBJECT-IMPOSED / SUBJECT FREE
And I would like the label (labels are in french) : MEMOIRE / SUJET IMPOSE / SUJET LIBRE
How can I get the label instead ? Thank you in advance.
In single.php :
$post_queried = get_queried_object();
$context['post_type_current'] = get_post_type_object(get_post_type($post_queried));
$get_post_current_user = get_posts( array(
'author' => $current_user->ID,
'post_type' => array('dissertation', 'subject-imposed', 'subject-free'),
));
$context['list_posts_current_user'] = $get_post_current_user;
In single.twig :
<ul class="menu-secondary__list">
{% for item in list_posts_current_user %}
{% set active = (post_type_current.name == item.post_type) ? 'active' : '' %}
<li class="menu-secondary__item">{{ item.post_type }}</li>
{% endfor %}
</ul>
Two steps to fix this one, first ...
Convert into an array of Timber\Posts
$get_post_current_user is an array of WP_Posts we need to convert these to Timber\Posts in order to interact with Timber's methods. Many ways to do this, but the easiest ...
{% for item in Post(list_posts_current_user) %}
Access the type method:
Now you can use the Post::type() method in your Twig template and access the labels:
{{ item.type.labels.name }}
The {{ item.type }} property will give you everything from here:
https://developer.wordpress.org/reference/functions/get_post_type_object/

Show html table in drupal 8 block

i have created a module in Drupal 8 for showing third party API data in a block
here is some data returned from that API
{"ObjectId":43,"ObjectName":"MEGA MELA","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-22T07:56:03.767","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null},{"ObjectId":44,"ObjectName":"Event x11","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-23T00:33:50.7","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null}]}
i have created a custom module to show some of this data in a block
this is my module directory hierarchy , module directory name is apihtml
-apihtml
-src
-Plugin
-Block
-rest.php
-apihtml.info.yml
as you can see there are just two files
apihtml.info.yml and rest.php
here is apihtml.info.yml content
name: Third Party Api Data with html
type: module
description: 'This is for showing rest data in ui with html'
package: Custom
version: 8.x
core: 8.x
dependencies:
- node
- block
and here is rest.php content
<?php
/**
* #file
*/
namespace Drupal\apihtml\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Component\Serialization\Json;
/**
* Creates a 'Foobar' Block
* #Block(
* id = "AntShow with html formatting",
* admin_label = #Translation("Ant Show & HTML"),
* )
*/
class rest extends BlockBase {
public function build() {
/** #var \GuzzleHttp\Client $client */
$client = \Drupal::service('http_client_factory')->fromOptions([
'base_uri' => 'http://myApiPath',
]);
$response = $client->get('objects/events');
$dec = Json::decode($response->getBody());
$items = [];
foreach ($dec as $d) {
foreach ($d as $ins) {
$items[] = $ins['ObjectName'] ;
}
}
return [
'#theme' => 'item_list',
'#items' => $items,
];
}
}
here by using array Key ObjecName i am able to show object name in block
here is the block OutPut
MEGA MELA
Event x11
but i want something more
i want to show this API returned data in the tabular form means the array key should be the header of the data value data will be in rows
like this
ObjectId | ObjectName | ObjectTitle | ObjectDescription | ObjectLabel | ObjectTypeId | MaxFieldsExpected | ObjectValueType | ObjectControlType | IsDeleted | CreatedDate | CreatedBy | EditedDate | EditedBy
43 | MEGA MELA | Event Created by API | NEW EVENT BY API | ..............................................................................
in this Drupal block all the data displayed has to be returned in build function in rest.php file
this is the code which shows data returned by api in tabular format
foreach ($dec as $d) {
?>
<table>
<?php foreach ($d as $ins) { ?>
<tr>
<?php
foreach ($ins as $key=>$value) {
echo "<td><h3>".$key."</h3></td>";
} ?>
</tr>
<tr>
<?php
foreach ($ins as $key=>$value) {
echo "<td><h3>".$value."</h3></td>";
} ?>
</tr><?php
} ?>
</table> <?php
}
But i am not getting how can i put this code in build function of rest.php to show tabular data in the block
Use custom Template:
1. change you return :
return [
'#theme' => 'item_list',
'#items' => $items,
];
return [
'#theme' => 'my_custom_template',
'#content' => $items,
];
in your apihtml.module us hook_theme
function apihtml_theme() {
return array(
'my_custom_template' =>[
'variables' => [
'content' => []
]
]);
}
and create a custom twig: templates/my-custom-template.html.twig
{% for data in content %}
...
{% endfor %}
You can create custom Twig template and display all your data as you want. You can read more about it here.

In cakephp 3 I got error Unexpected field in POST data

In cakephp 3 I got error Unexpected field in POST data.
Actually that field is not in my table, but I want to use in controller.
The Security Component in CakePHP is not forgiving. If you want to allow a field thru that should not go thru the Security Component hashing process, you need to use the unlockedField method that comes with the FormHelper class as such:
$this->Form->unlockField('field');
If this does not work, you will need to provide us with the pertinent code
I was getting the similar error in cakephp 3.4 I was using the simple html form and input fields. I was passing the input fields data in array. like below:-
<form action="" method="post">
<input name="data[1][category_1]" id="category_1">
</form>
Then i do some R&D and found that we need to use the cakephp form helper to create the form and its fields like below :-
In case of pass form data in array
<?= $this->Form->create($user, ['url' => ['controller' => 'Users', 'action' => 'saveOrder']]); ?>
<?= $this->Form->input("Data.1.category_1"); ?>
<?= $this->Form->end() ?>
In case of simple input fields you can do the code like below
<?= $this->Form->create($user, ['url' => ['controller' => 'Users', 'action' => 'saveOrder']]); ?>
<?= $this->Form->input("category"); ?>
<?= $this->Form->end() ?>
This work form me and resolve the error Unexpected field in POST data in cakephp 3.4

Angular ui-select : Adding custom ids

I am using angular ui-select.
<ui-select ng-model="selected" theme="select2">
<ui-select-match placeholder="Select">{{$select.selected.name}}
</ui-select-match>
<ui-select-choices
repeat="student in studentList | filter: $select.search"
ng-class="{{student.name}} == 'ABC' ? 'found' : 'not_Found' ">
{{student.name}}
</ui-select-choices>
</ui-select>
Can we add custom ids to each of the new li formed from the ui-select-choices?
If yes then how?
Yes, using the ugly but standard Angular repeat syntax:
repeat="student.id as student in studentList | ..."

Using doctrine database object in a template

I am new to Symfony and am finally beginning to understand how to query a database using a Doctrine. However, I am lost as far understanding how to use the database object content in a Twig template.
Lets say my database object contains product Id's, names, prices, for 50 different products. After I am done querying the database in the controller, I do the following, to pass the database object into the Twig template:
public function searchAction($word)
{
//query database using the $word slug and prepare database object accordingly
$dataObject; // contains query results
return $this->render('GreatBundle:Default:search.html.twig', array('word' => $word));
}
This is where I am stuck. Now I have a Twig template, I would like to pass the DB object from the controller and then print out the database data in my Twig template.
I appreciate any suggestions as to how I can accomplish this.
Many thanks in advance!
I'll respond with an example (more easier for me to explain)
You want to search something with a slug (the var $word in your example). Let's say you want to find a article with that.
So your controller :
public function searchAction($word)
{
//query database using the $word slug and prepare database object accordingly
// Search the list of articles with the slug "$word" in your model
$articleRepository = $this->getDoctrine()->getRepositoy('GreatBundle:Article');
$dataObject = $articleRepository->findBySlug($word);
// So the result is in $dataObject and to print the result in your twig, your pass the var in your template
return $this->render('GreatBundle:Default:search.html.twig', array('result' => $dataObject));
}
The twig template 'GreatBundle:Default:search.html.twig'
{% for item in result %}
{{ item.title }} : {{ item.content }}
{% endfor %}
Just look the second example in the Symfony2 Book (Sf2 Book - templating), you have to use the function "for" to parse your object (like an array in php !)
Example in your twig template :
{% for item in word %}
{{ item.id }} - {{ item.name }} - {{ item.description }}{# etc... #}<br>
{% else %}
<h2>Aoutch ! No data !</h2>
{% endfor %}
Ah, and it's not the good var in your render method (but it's was for your example !)
public function searchAction($word)
{
//query database using the $word slug and prepare database object accordingly
$dataObject; // contains query results
return $this->render('GreatBundle:Default:search.html.twig', array('word' => $dataObject));
}

Resources