I created a parameter in my parameters.yml file:
parameters:
category:
var: test
How can I access this parameter in my config.yml? For example, I want to pass this parameter to all my twig files as a global twig variable:
twig:
globals:
my_var: %category.var% # throws ParameterNotFoundException
In all the symfony config files I've seen, the entries under 'parameters:' have always been fully qualified. I don't fully understand why this is but it may help you to write the entries in your parameters.yml like this:
category1.var1: xxx
category1.var2: yyy
category1.var3. zzz
category2.subcategory1.var1: 5
category2.subcategory1.var2: 10
category2.subcategory2.var1: foo
category2.subcategory2.var2: bar
... and so on.
EDIT
I tried pasting the nested parameter from the question into parameters.local.yml in one of my projects and ran a trivial unit test to retrieve that and a fully qualified parameter from the container e.g.
$testUserEmail = $container->getParameter('test.user.email');
$this->assertEquals('dahlia.flower#randomtest.com', $testUserEmail);
$testParam = $container->getParameter('category.var');
$this->assertEquals('test', $testParam);
The fully qualified parameter was fine, attempting to get the nested parameter resulted in an InvalidArgumentException: The parameter category.var must be defined. I don't think parameters can be defined with nesting.
$this->container->getParameter('category')['var']
I have tested that on symfony 2.8 and it worked for me.
Or you can write simple ParameterBag which can implement nested parameters dynamically (without config values duplicity):
<?php
namespace Your\Namespace;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
class ParameterBagNested extends ParameterBag
{
/**
* wire $this->set() logic into add() too
*
* #param array $parameters
*/
public function add( array $parameters )
{
foreach ( $parameters as $name => $value ) {
$this->set( $name, $value );
}
}
/**
* sets all levels of nested array parameters with dot notation
* - loggly[host: loggly.com] will be translated this way:
* - loggly: [host: loggly.com] - standard array parameter will be left as is
* - loggly.host: loggly.com - nested variables ar translated so you can access them directly too as parent.variable
*
* #param string $name
* #param mixed $value
*/
public function set( $name, $value )
{
if ( $this->has( $name ) ) {
// this is required because of array values
// we can have arrays defined there, so we need to remove them first
// otherwise some subvalues would to remain in the system and as a result, arrays would be merged, not overwriten by set()
$this->remove( $name );
}
$this->setNested( $name, $value );
}
/**
* remove checks even if name is not array
*
* #param string $name
*/
public function remove( $name )
{
$value = $this->get( $name );
if ( is_array( $value ) ) {
foreach ( $value as $k => $v ) {
$this->remove( $name . '.' . $k, $v );
}
}
if ( strpos( $name, '.' ) !== FALSE ) {
$parts = explode( '.', $name );
$nameTopLevel = reset( $parts );
array_shift( $parts );
$topLevelData = $this->removeKeyByAddress( $this->get( $nameTopLevel ), $parts );
ksort( $topLevelData );
$this->setNested( $nameTopLevel, $topLevelData );
}
parent::remove( $name );
}
/**
* #param array $data
* #param array $addressParts
*
* #return array
*/
private function removeKeyByAddress( $data, $addressParts )
{
$updatedLevel = & $data;
$i = 1;
foreach ( $addressParts as $part ) {
if ( $i === count( $addressParts ) ) {
unset( $updatedLevel[$part] );
} else {
$updatedLevel = & $updatedLevel[$part];
$i++;
}
}
return $data;
}
/**
* #see set()
*
* #param string $name
* #param mixed $value
*/
private function setNested( $name, $value )
{
if ( is_array( $value ) ) {
foreach ( $value as $k => $v ) {
$this->setNested( $name . '.' . $k, $v );
}
}
parent::set( $name, $value );
}
}
phpunit Test:
<?php
namespace Your\Namespace;
use Symfony\Component\DependencyInjection\Tests\ParameterBag\ParameterBagTest;
/**
* its essential to use ParameterBagNested as ParameterBag because this way we run even parent class tests upon it
* parent class is part of Symfony DIC standard test suite and we use it here just for check if our parameter bag is still ok
*/
use SBKS\DependencyInjection\ParameterBag\ParameterBagNested as ParameterBag;
/**
* testing basic and even added ParameterBag functionality
*/
class ParameterBagNestedTest extends ParameterBagTest
{
public function testConstructorNested()
{
$bag = new ParameterBag(
array(
'foo' => array( 'foo1' => 'foo' ),
'bar' => 'bar',
)
);
$this->assertEquals(
array(
'foo.foo1' => 'foo',
'foo' => array(
'foo1' => 'foo',
),
'bar' => 'bar',
),
$bag->all(),
'__construct() takes an array of parameters as its first argument'
);
}
public function testRemoveNested()
{
$bag = new ParameterBag(
array(
'foo' => array(
'foo1' => array(
'foo11' => 'foo',
'foo12' => 'foo',
),
'foo2' => 'foo',
),
'bar' => 'bar',
)
);
$bag->remove( 'foo.foo1.foo11' );
$this->assertEquals(
array(
'foo' => array(
'foo1' => array(
'foo12' => 'foo',
),
'foo2' => 'foo',
),
'foo.foo1' => array( 'foo12' => 'foo' ),
'foo.foo1.foo12' => 'foo',
'foo.foo2' => 'foo',
'bar' => 'bar',
),
$bag->all(),
'->remove() removes a parameter'
);
$bag->remove( 'foo' );
$this->assertEquals(
array(
'bar' => 'bar',
),
$bag->all(),
'->remove() removes a parameter'
);
}
public function testSetNested()
{
$bag = new ParameterBag(
array(
'foo' => array(
'foo1' => array(
'foo11' => 'foo',
'foo12' => 'foo',
),
'foo2' => 'foo',
),
)
);
$bag->set( 'foo', 'foo' );
$this->assertEquals( array( 'foo' => 'foo' ), $bag->all(), '->set() sets the value of a new parameter' );
}
public function testHasNested()
{
$bag = new ParameterBag(
array(
'foo' => array(
'foo1' => array(
'foo11' => 'foo',
'foo12' => 'foo',
),
'foo2' => 'foo',
),
)
);
$this->assertTrue( $bag->has( 'foo' ), '->has() returns true if a parameter is defined' );
$this->assertTrue( $bag->has( 'foo.foo1' ), '->has() returns true if a parameter is defined' );
$this->assertTrue( $bag->has( 'foo.foo1.foo12' ), '->has() returns true if a parameter is defined' );
$this->assertTrue( $bag->has( 'foo.foo2' ), '->has() returns true if a parameter is defined' );
}
}
Then you can use it by injecting Parameter bag into ContainerBuilder:
$parameterBag = new \Your\Namespace\ParameterBagNested();
$container = new ContainerBuilder($parameterBag);
Thats all, you can use nested parameter with dot notation now in Symfony DI container.
If you are using Symfony plugin in phpstorm it will autocomplete your nested attributes with dot notation too.
A bit late, but here is a solution that worked for me.
# app/config/config.yml
twig:
globals:
my_var: category['var']
Explanation
When you import your parameters file into your app/config/config.yml file, you can automatically access all variables defined in your parameters file.
Keep in mind, when you use the structure:
# app/config/parameters.yml
parameters:
category:
var: test
You are defining a parameter called category with a value that is in turn itself a key-value pair array that contains var as key and test as value.
Update
In newer versions of symfony (3.4 - 4.2) you can now do the following:
# app/config/config.yml
twig:
globals:
custom_categories: '%categories%'
By having this parameter set up:
# app/config/parameters.yml
parameters:
categories:
- category_A
- category_B
Things to note here:
- in parameters.yml categories is an array
so to use it in a twig template, you should be able to do something like:
<ul>
{% for category in categories %}
<li> {{ category }} </li>
{% endfor %}
</ul>
Another example with objects:
# app/config/parameters.yml
parameters:
marketplace:
name: 'My Store'
address: '...'
Configuring twig variables:
# app/config/config.yml
twig:
globals:
marketplace: '%marketplace%'
Using it in twig:
...
<p>{{marketplace.name}}</p>
<p>{{marketplace.address}}</p>
...
Hope this helps! :)
Symfony's expression language syntax to the rescue! Give this a shot:
twig:
globals:
my_var: '#=parameter("category")["var"]'
To provide an alternative approach that doesn't require removing values from the setter, I override the getter method instead.
namespace NameSpaceFor\ParameterBags;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
class ParameterBagParser extends ParameterBag
{
/**
* {#inheritDoc}
*/
public function get($name, $parent = null)
{
if (null === $parent) {
$parent = $this->parameters;
}
$name = strtolower($name);
if (!array_key_exists($name, $parent)) {
if (!$name) {
throw new ParameterNotFoundException($name);
}
if (false !== strpos($name, '.')) {
$parts = explode('.', $name);
$key = array_shift($parts);
if (isset($parent[$key])) {
return $this->get(implode('.', $parts), $parent[$key]);
}
}
$alternatives = [];
foreach ($parent as $key => $parameterValue) {
$lev = levenshtein($name, $key);
if ($lev <= strlen($name) / 3 || false !== strpos($key, $name)) {
$alternatives[] = $key;
}
}
throw new ParameterNotFoundException($name, null, null, null, $alternatives);
}
return $parent[$name];
}
}
It recursively traverses through the name until it is all of the dot notations have been checked.
So it will work with arrays and scalar values.
config.yml:
parameters:
my_param:
- test
my_inherited: '%my_param.0%' #test
ContainerAware:
$container->getParameter('my_param')[0]; //test
Trick to emulate nested parameters, acccess in the yaml file:
parameters:
crawler:
urls:
a: '%crawler.urls.a%' # here the root of the rest of the tree/array
crawler.urls.a:
cat1:
- aa
- bb
cat2:
- cc
services:
xx:
class: myclass
arguments:
$urls: '%crawler.urls.a%'
In Symfony I now access the parameter('crawler') as complete tree, in service xx the subtree/array is accessible.
Related
Let's say you create a WordPress post and assign it a meta key foo with value bar. You only want to display an ACF field if foo is equal to bar. However, there's no built-in location rule in ACF to do this. How would you solve this problem by creating an ACF custom location rule?
In order to display ACF fields if a post meta key is equal or not equal to some value, use the following snippet. It's based off of the ACF Custom Location Rules guide.
if( ! defined( 'ABSPATH' ) ) exit;
class My_ACF_Location_Post_Meta extends ACF_Location {
public function initialize() {
$this->name = 'post_meta';
$this->label = __( "Post Meta", 'acf' );
$this->category = 'post';
$this->object_type = 'post';
}
public function rule_values($choices, $rule){
if(!acf_is_screen('acf-field-group') && !acf_is_ajax('acf/field_group/render_location_rule')){
return array(
$rule['meta_key'] => $rule['meta_key'],
$rule['meta_value'] => $rule['meta_value']
);
}
ob_start();
acf_render_field(array(
'type' => 'text',
'name' => 'meta_key',
'prefix' => 'acf_field_group[location]['.$rule['group'].']['.$rule['id'].']',
'value' => (isset($rule['meta_key']) ? $rule['meta_key'] : ''),
'placeholder' => 'Meta Key'
));
acf_render_field(array(
'type' => 'text',
'name' => 'meta_value',
'prefix' => 'acf_field_group[location]['.$rule['group'].']['.$rule['id'].']',
'value' => (isset($rule['meta_value']) ? $rule['meta_value'] : ''),
'placeholder' => 'Meta Value'
));
return ob_get_clean();
}
public function rule_operators($choices, $rule){
$choices = [];
$choices['key_is_equal_to_value'] = __('key is equal to value', 'acf');
$choices['key_is_not_equal_to_value'] = __('key is not equal to value', 'acf');
return $choices;
}
public function match( $rule, $screen, $field_group ) {
// Check screen args for "post_id" which will exist when editing a post.
// Return false for all other edit screens.
if( isset($screen['post_id']) ) {
$post_id = $screen['post_id'];
} elseif (isset($screen['attachment_id'])) {
$post_id = $screen['attachment_id'];
} else {
return false;
}
// Load the post meta object for this edit screen.
$post_meta_value = get_post_meta( $post_id, $rule['meta_key'], true );
if( !$post_meta_value ) {
return false;
}
// Compare the Post's meta value to rule meta value.
$result = ( strval($post_meta_value) == $rule['meta_value'] );
// Return result taking into account the operator type.
if( $rule['operator'] == 'key_is_not_equal_to_value' ) {
return !$result;
}
return $result;
}
}
add_action('acf/init', 'my_acf_init_location_types');
function my_acf_init_location_types() {
// Check function exists, then include and register the custom location type class.
if( function_exists('acf_register_location_type') ) {
acf_register_location_type( 'My_ACF_Location_Post_Meta' );
}
}
I am using Drupal 9. I want to give the ability to the admin to place a block and select from the block a taxonomy term in which will filter a content type.
I did the above by creating a custom block with the taxonomy "Sponsor Type" as you can see the screenshot bellow.
Also I use views_embed_view to pass the taxonomy as an argument and filter the data using Contextual filters
Custom block code:
<?php
namespace Drupal\aek\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a 'SponsorsBlock' block.
*
* #Block(
* id = "sponsors_block",
* admin_label = #Translation("Sponsors"),
* )
*/
class SponsorsBlock extends BlockBase {
/**
* {#inheritdoc}
*/
public function defaultConfiguration() {
return [
"max_items" => 5,
] + parent::defaultConfiguration();
}
public function blockForm($form, FormStateInterface $form_state) {
$sponsors = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadTree("horigoi");
$sponsorsOptions = [];
foreach ($sponsors as $sponsor) {
$sponsorsOptions[$sponsor->tid] = $sponsor->name;
}
$form['sponsor_types'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Sponsor Type'),
'#description' => $this->t('Select from which sponsor type you want to get'),
'#options' => $sponsorsOptions,
'#default_value' => $this->configuration['sponsor_types'],
'#weight' => '0',
];
$form['max_items'] = [
'#type' => 'number',
'#title' => $this->t('Max items to display'),
'#description' => $this->t('Max Items'),
'#default_value' => $this->configuration['max_items'],
'#weight' => '0',
];
return $form;
}
/**
* {#inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['sponsor_types'] = $form_state->getValue('sponsor_types');
$this->configuration['max_items'] = $form_state->getValue('max_items');
}
/**
* {#inheritdoc}
*/
public function build() {
$selectedSponsorTypes = $this->configuration['sponsor_types'];
$cnxFilter = '';
foreach ($selectedSponsorTypes as $type) {
if ($type !== 0) {
$cnxFilter .= $type . ",";
}
}
return views_embed_view('embed_sponsors', 'default', $cnxFilter);
}
}
My issue now is how to limit the results. If you look above I have added an option "Max items to display" but using the Contextual filters I can't find any way to pass that argument to handle this.
If you use views_embed_view(), you will not be able to access the view object.
Load your view manually then you can set any properties before executing it:
use Drupal\views\Views;
public function build() {
$selectedSponsorTypes = $this->configuration['sponsor_types'];
$cnxFilter = '';
foreach ($selectedSponsorTypes as $type) {
if ($type !== 0) {
$cnxFilter .= $type . ",";
}
}
$view = Views::getView('embed_sponsors');
$display_id = 'default';
$view->setDisplay($display_id);
$view->setArguments([$cnxFilter]);
$view->setItemsPerPage($this->configuration['max_items']);
$view->execute();
return $view->buildRenderable($display_id);
}
My category url contains both id and slug like https://myapp.com/category/56-category-name (id field = 56 and slug field = category-name in the DB), when updating category name the slug field in DB is updated but the id still the same.
I would like to display my category whatever the slug provided that the id is correct and of course display the correct url. In this case SEO still correct i think.
Here my show method :
/**
* #Route("/category/{id}-{slug}", name="category_show", requirements={"id"="\d+"})
*/
public function show(CategoryRepository $categoryRepository, $slug, $id): Response
{
$category = $categoryRepository->find($id);
if($category->getSlug() !== $slug) {
return $this->redirectToRoute('category_show', [
'id' => $id,
'slug' => $category->getSlug()
]);
}
return $this->render('category/show.html.twig', [
'category' => $category
]);
}
It works if the given id exists in DB, othewise i got an error Call to a member function getSlug() on null. I can throw a NotFoundException, but i think this method use many times queries.
So please help me doing these properly with more flexibility and performance.
In case I would like to display the category in the post url as well like https://myapp.com/56-category-name/23-post-title how to go about it ??
Thanks in advance
Use findOneBy() and check if the result is null
/**
* #Route("/category/{id}-{slug}", name="category_show", requirements={"id"="\d+"})
* #Route("/{id}-{slug}/{idArticle}-{postSlug}", name="article_show", requirements={"idArticle"="\d+"})
*/
public function show(CategoryRepository $categoryRepository, $slug, $id, $idArticle = false, $postSlug = false ): Response
{
$category = $categoryRepository->findOneBy(['id'=>$id]);
if(is_null($category)){
throw new NotFoundHttpException(); //404, nothing found
}else{
//Category found.
if($idArticle){ // https://myapp.com/56-category-name/23-post-title
//Article route, put your logic here
}else{ //https://myapp.com/category/56-category-name
// Category route, logic here
if($category->getSlug() !== $slug) {
return $this->redirectToRoute('category_show', [
'id' => $id,
'slug' => $category->getSlug()
]);
}
return $this->render('category/show.html.twig', [
'category' => $category
]);
}
}
}
here is what i found good for my app :
in my CategoryController.php i create, the show method :
/**
* #Route("/{id}-{slug}", name="category_show", requirements={"id"="\d+"})
*/
public function show(CategoryRepository $categoryRepository, $slug = null, $id): Response
{
$category = $categoryRepository->find($id);
if (!$category) {
throw new NotFoundHttpException();
}
if($category->getSlug() !== $slug) {
return $this->redirectToRoute('category_show', [
'id' => $id,
'slug' => $category->getSlug()
]);
}
return $this->render('category/show.html.twig', [
'category' => $category
]);
}
in my index template to display the category_show link :
show
in my ArticleController.php i create, the show method :
/**
* #Route("/{category}/{id}-{slug}", name="article_show", requirements={"id"="\d+"})
*/
public function show(ArticleRepository $articleRepository, $slug = null, $id, $category = null): Response
{
$article = $articleRepository->find($id);
if (!$article) {
throw new NotFoundHttpException();
}
$cat = $article->getCategory()->getId() . '-' . $article->getCategory()->getSlug();
if($article->getSlug() !== $slug || $cat !== $category) {
return $this->redirectToRoute('article_show', [
'id' => $id,
'slug' => $article->getSlug(),
'category' => $cat
]);
}
return $this->render('article/show.html.twig', [
'article' => $article,
]);
}
in my index template to display the article_show link :
show
For me that solves my problem. But still, if we can improve the process, I'm a buyer.
Thanks
Hi I would like to create multiple blocks using the data rendered from variable table in Drupal 8.
I was able to achieve it in Drupal 7 but can't find a way to do the same in Drupal 8.
Can anyone suggest me a way to achieve this.
Code for Drupal 7 is as follows:
<?php
/**
* Create a block that will display the calendar feed
* Implements hook_block_info
*/
function my_module_block_info() {
$blocks = array();
$block_urls = variable_get('my_module_content');
$block_regions = variable_get('my_module_content_block_region');
$number_of_blocks = count($block_urls);
if ( $number_of_blocks > 0 ) {
foreach ( $block_urls as $key => $block_url ) {
$blocks['eventblock-' . $key] = array(
'info' => t('my_module_widget_block_' . $key),
'status' => TRUE,
'region' => $block_regions[$key],
'cache' => DRUPAL_NO_CACHE,
);
}
}
return $blocks;
}
/**
* Render the my_module block
* Implements hook_block_view
*/
function my_module_block_view($delta = '') {
$block = array();
$block_urls = variable_get('my_module_content' , array());
foreach ( $block_urls as $key => $block_url ) {
switch ($delta) {
case 'eventblock-' . $key:
$widgetURL= $block_urls[$key];
$block['content'] = my_module_content_generate($widgetURL); // Some function to generate block content.
break;
}
}
return $block;
}
I have just started with REST API and using it for creating posts from frontend. I managed to publish post with Title, Excerpt, Content.
I want to add a Custom Meta Field value aswell, any example or help is much appreciated.
This is my Ajax Code, all other fields working fine except meta value is not being added in post
jQuery( document ).ready( function ( $ ) {
$( '#post-submission-form' ).on( 'submit', function(e) {
e.preventDefault();
var title = $( '#post-submission-title' ).val();
var excerpt = $( '#post-submission-excerpt' ).val();
var content = $( '#post-submission-content' ).val();
var status = 'draft';
var data = {
title: title,
excerpt: excerpt,
content: content,
status: status,
meta: {
'video_url_url' : 'abc',
}
};
$.ajax({
method: "POST",
url: POST_SUBMITTER.root + 'wp/v2/posts',
data: data,
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', POST_SUBMITTER.nonce );
},
success : function( response ) {
console.log( response );
alert( POST_SUBMITTER.success );
},
fail : function( response ) {
console.log( response );
alert( POST_SUBMITTER.failure );
}
});
});
} );
Add this to your functions.php :
/**
* Add the meta fields to REST API responses for posts read and write
* Read and write a post meta fields in post responses
*/
function mg_register_meta_api() {
//Meta Fields that should be added to the API
$meta_fields = array(
'video_url_url',
'another_meta_key'
);
//Iterate through all fields and add register each of them to the API
foreach ($meta_fields as $field) {
register_rest_field( 'ring',
$field,
array(
'get_callback' => array( $this, 'mg_fw_get_meta'),
'update_callback' => array( $this, 'mg_fw_update_meta'),
'schema' => null,
)
);
}
}
add_action( 'rest_api_init', 'mg_register_meta_api' );
/**
* Handler for getting custom field data.
*
* #since 0.1.0
*
* #param array $object The object from the response
* #param string $field_name Name of field
*
* #return mixed
*/
function mg_get_meta( $object, $field_name ) {
return get_post_meta( $object[ 'id' ], $field_name );
}
/**
* Handler for updating custom field data.
*
* #since 0.1.0
* #link http://manual.unyson.io/en/latest/helpers/php.html#database
* #param mixed $value The value of the field
* #param object $object The object from the response
* #param string $field_name Name of field
*
* #return bool|int
*/
function mg_update_meta( $value, $object, $field_name ) {
if ( ! $value || ! is_string( $value ) ) {
return;
}
return update_post_meta( $object->ID, $field_name, maybe_serialize( strip_tags( $value ) ) );
}
Now you should be able to read and write for meta 'video_url_url' using the api.
I was playing around with this today and came up with the following solution that works well and adds multiple fields.
Hope this helps anyone who may be stuck on this.
add_action( 'rest_api_init', 'foo_register_meta_api' );
function foo_register_meta_api() {
// Meta Fields to add to the API
$meta_fields = array(
'field_1',
'foo_bar',
'foobar',
'another_field'
);
// Loop all fields and register each of them to the API
foreach ($meta_fields as $field) {
register_rest_field( 'my-post-type',
$field,
array(
'get_callback' => function ($params) use ($field) {
return \get_post_meta($params['id'], $field);
},
'update_callback' => function ($value, $object, $fieldName){
return \update_post_meta($object->ID, $fieldName, $value);
},
'schema' => null
)
);
}
}
I am trying to save phy_marks in post type candidates. What I've got is creating the post but not saving phy_marks.
My JSON data:
{ "title": "Why not working finally","content": "Test", "status": "publish" ,"post_type": "candidates", "meta": {
"phy_marks": 66 } }
My Code:
add_action( 'rest_api_init', 'foo_register_meta_api' );
function foo_register_meta_api() {
// Meta Fields to add to the API
$meta_fields = array(
'phy_marks'
);
// Loop all fields and register each of them to the API
foreach ($meta_fields as $field) {
register_rest_field( 'candidates',
$field,
array(
'get_callback' => function ($params) use ($field) {
return \get_post_meta($params['id'], $field);
},
'update_callback' => function ($value, $object, $fieldName){
return \update_post_meta($object->ID, $fieldName, $value);
},
'schema' => null
)
);
}
}