get_fields is empty after inserting an ACF gutenberg block - advanced-custom-fields

we just integrated timber into our acf-gutenberg-block setting, and we love it. ;)
There is one thing that doesnt work.
Our ACF Blocks have prefilled values like a "dummy" headline.
But when a backend user adds a block into the gutenberg view, the get_fields() inside our render_callback function is empty. The user then changes the headline and then get_fields brings up all the fields and their values.
any idea how to fix this problem on the first load of an acf block?
the callback function looks like this (just like the one in the documentation)
function my_acf_block_render_callback($block, $content = '', $is_preview = false)
{
$context = Timber::context();
// Store block values.
$context['block'] = $block;
// Store field values.
$context['fields'] = get_fields();
// Value for detecting editor-view in twig (like is admin())
$context['is_preview'] = $is_preview;
// Render the block.
Timber::render('blocks/' . $block['template-name'] . '.html.twig', $context);
}
cheers,
ToM

Related

WordPress: Calling a shortcode for a value in a different shortcode

I'm very new to this, so please go easy on me if this is a dumb question.
I am building a site with events manager and geo my WordPress plugins. I want a user to be able to input their autofill location (via GMW) and have the calendar in EM output only events a certain distance from that location. I have already gotten (with handholding) to a point where I have a shortcode that spits out the coordinates of the location entered. The EM full calendar shortcode takes an attribute called 'near' which takes coordinates and subsequently outputs the desired calendar.
Code at the moment:
[fullcalendar near=[gmw_current_latlng] near_distance=20]
with [gmw_current_latlng] normally returning the lat and long separated by a comma. Normally, the near att takes say 50.111123,-45.234, etc.
My problem is that it seems that I cannot get what I want with this hardheaded approach. Again, I'm very new to coding and don't know much, but I've been working on this problem for weeks and have not found an answer. I've tried many different routes, but this way has brought me oh so close to where I want to be.
The GMW dev said this about the problem:
"The thing is that I am not even sure if you can pass a value to a
shortcode using another shortcode. I’ve never tried this myself. The
best way would be to use filters and a custom function to “inject” the
coords directly into the calendar function."
If he is right and it's not possible, I have no idea how to carry out his second suggestion. Hopefully, I can get this sorted out because frankly, my site depends on it working.
As #Jeppe mentioned, you can do Nested Shortcodes:
[shortcode_1][shortcode_2][/shortcode_1]
But the parser does not like shortcode values as attributes in other shortcodes.
It sounds like you're reliant on a few plugins and their shortcodes, so I don't suggest editing those shortcodes - but if you look at the Shortcode API it's pretty easy to add you own. For simplicity's sake, this example won't contain the "proper" methods of making sure the shortcodes exist/plugins are installed etc, and will just assume they are.
// Register a new shortcode called [calendar_with_latlng]
add_shortcode( 'calendar_with_latlng', 'calendar_with_latlng_function' );
// Create the function that handles that shortcode
function calendar_with_latlng_function( $atts ){
// Handle default values and extract them to variables
extract( shortcode_atts( array(
'near_distance' => 20
), $atts, 'calendar_with_latlng' ) );
// Get the current latlng from the gmw shortcode
$gmw_current_latlng = do_shortcode( '[gmw_current_latlng]' );
// Drop that value into the fullcalendar shortcode, and add the default (or passed) distance as well.
return do_shortcode( '[fullcalendar near='. $gmw_current_latlng .' near_distance='. $near_distance .']' );
}
Provided [gmw_current_latlng] returns a useable format for your [fullcalendar] shortcode, you should now be able to use your new shortcode that combines the two: [calendar_with_latlng] or you can also add the near_distance attribute: [calendar_with_latlng near_distance=44].
You would just need to put the above functions into your functions.php, create a Simple Plugin, or save them to a file and add it in your Must-Use Plugins directory.
Of course you can pass a shortcode as an attribute of another shortcode. The only problem is, attributes doesn't pass [ or ]. So you have replace convert those bracket them with their html entry.
Replace [ with [ and ] with ] and you should be fine. Here is an example.
function foo_shortcode( $atts ) {
$a = shortcode_atts( array(
'foo' => "Something",
'bar' => '',
), $atts );
$barContent = html_entity_decode( $atts['bar'] );
$barShortCodeOutput = do_shortcode($barContent);
return sprintf("Foo = %s and bar = %s", $a['foo'], $barShortCodeOutput);
}
add_shortcode( 'foo', 'foo_shortcode' );
function bar_shortcode( $atts ) {
return "Output from bar shortcode";
}
add_shortcode( 'bar', 'bar_shortcode' );
Then put this on your editor
[foo bar=[bar] ]
See we are passing a shortcode [bar] as an attribute of [foo]. So the output should be -
Foo = Something and bar = Output from bar shortcode
I know it looks a bit nasty but it will do the trick.

angularjs save rendered values in html in a variable

I hope someone can help me with this, It's a strange question maybe as I didn't find an answer online.
I call the database and retrieve a list (in json) of items.
Then in angularjs,I render this list by extracting relevant pieces of data(name,age,etc) and show it properly in a table as a list of rows.
I have then an edit button that takes me to another page where I want to put a dropdown list.
What I want to know if is possible to add to that dropdown list the rendered list I previously created in my previous page.
is it possible to save the previously rendered list in a variable and then use that variable in the dropdown?
thank you
You could store the list within a controller and make this data availablte to this dropdown, I think.
Instead of trying to query for the list, add the list to the template, get the list from the template and render somewhere else, I'd suggest query for the list, save the list in a service , and then when you want to use that list again, get it from the service. Something like:
service:
var services = angular.module('services');
services.factory('getListService',['$http',function($http){
var getListOfStuff = function(){
//call to database
return //your json
};
var extractNameAgeEtc = function(){
var myListOfStuff = //get list of stuff from $http or database
var myListOfNameAgeEtc = //make a list of tuples or {name,age,etc} objects
return myListOfNameAgeEtc;
};
return {
extractNameAgeEtc : extractNameAgeEtc
};
}]);
controllers:
angular.module('controllers',['services']);
var controllersModule = angular.module('controllers');
controllersModule.controller('tableRenderController',['getListService','$scope',function(getListService,$scope){
//use this with your table rendering template, probably with ng-repeat
$scope.MyTableValue = getListService.extractNameAgeEtc();
}]);
controllersModule.controller('dropdownRenderController',['getListService','$scope',function(getListService,$scope){
//use this with your dropdown rendering template, probably with ng-repeat
$scope.MyDropDownValue = getListService.extractNameAgeEtc();
}]);

Dynamic Tabs and dynamic content from mySQL

Using CodeIgniter, I have a set of tabs with titles taken from a table on my DB.
When I click a certain tab, it loads a content which is also
from another table on my DB.
I know the second part will need something like ajax. But I'm really confused on how to do this in an MVC way.
I tried creating a controller which would fetch the title of the tabs from my model, then load a view passing the data fetch from my model as params. Now, I created the tab, but dynamically changing the content when clicked is where I'm stuck.
Anyone can at least tell me the basic idea on how to achieve this?
Thank you so much!
lets look at this example may be it will help you
Tab 1
Tab 2
Tab 3
Tab 4
// where $id is value on which you have to execute query anfdfetech data from DB
// here is JS function with ajax call
<script type="text/javascript">
function fetchdata(id){
$.ajax({
type:"POST", url:"<?php echo base_url()?>controllername/functionname,
data:"&id=" + id,
complete:function (data) {
alert(data.responseText);
//or here what you want to do with ajax response, like put content in any html tag
$('#htmlidoftag').html(data.responseText);//like this
}
});
}
</script>
// here is the php function within controller
function functionname()
{
// First, delete old captchas
$id= $_REQUEST['id']
$response = $this->model->model_function($id);
// in model function run your query to fetch data
echo $response ;
}

html search form, how do i reduce the URL

I am creating a search for using the GET method at the moment, the problem is even if values are not selected and left at there default value they are sent and visible in the url.
I want to only have values selected in the url rather than every current value of the form, including default of 0.
the url ends up long and nasty:
search.php?search_shop_name=mcdonalds&search_shop_address=&search_total_rating=0&search_shop_comfort=0&search_shop_service=2&search_shop_ambience=0&search_shop_friendliness=0&search_shop_spacious=0&search_shop_experience=0&submit=#legend_total_results
I basically want to tidy up the url, am not actually sure if removing unwanted data is a good process or not, any possible advice on this situation? not sure if am being OCD with the visuals
Thanks
I would do the following. Using JavaScript or jQuery, create a submit function, in that function do a validate search with all fields if they = null then don't include them.
For e.g
var search;
function search(){
if(input[name=search].val() != null)
{
search = "q="+input[name=search].val();
}
if(input[name=search_shop_experience].val() != null)
{
search += "search_shop_experience="+input[name=ssearch_shop_experience].val();
}
}
I think you do .= or its += .

Magento: Add (and retrieve) custom database field for CMS pages

I want to assign custom parameters to CMS pages in Magento (i.e. 'about', 'customer service', etc), so they can be grouped.
The end goal is to use the parameters for each page to show (or hide) them in a nav menu. Writing a quick method in the page/html block to retrieve the pages (active only) for the menu was easy, but I can't figure out how to group them so that 'testimonials', 'history', and 'contact' are associated with 'about', and 'return policy', 'shipping', and 'contact' are associated with 'customer service'.
Any help to point me in the right direction would be greatly appreciated.
Thanks!
Unfortunately the cms pages are not entities and custom attributes are not supported.
My suggestion will be to overload Mage_Cms_Model_Page, create an associated table with your data, and in the new class constructor assign a new property to your_page object.
Perhaps just use URL key field in admin as your category denominator? E.g:
CMS page 1 URL key: about/testimonials
CMS page 2 URL key: about/history
CMS page 3 URL key: about/contact
CMS page 4 URL key: customer-service/return-policy
...
Then just loop through them in your template or have a method in a custom block class to group them together using regex based on the first part before slash.
Did you look for using of setData / getData for storing custom values? It works me well for custom blocks - I set value in controller and read it at block rendering.
I decided to go in a different direction with this. Here is what I did.
In admin interface:
Created static blocks for each page (i.e. 'Page: About')
Created a product category called CMS (URL Key: content), set Is Active to 'no'
Created child categories for each content category (i.e. 'about'). Set Is Active to 'yes' and Display Mode to 'Static block only' and selected corresponding static block.
Created child categories for each content category as above.
In code:
Created two new methods in (local copy of) Catalog/Block/Navigation.php to get the current parent category and its children:
public function getNavCategory($category)
{
if($category->getLevel() == 3){
return $category;
} else {
$parentCategory = Mage::getModel('catalog/category')->load($category->getParentId());
return $parentCategory;
}
}
public function getNavChildCategories($category)
{
$layer = Mage::getSingleton('catalog/layer');
/* #var $category Mage_Catalog_Model_Category */
$categories = $category->getChildrenCategories();
$productCollection = Mage::getResourceModel('catalog/product_collection');
$layer->prepareProductCollection($productCollection);
$productCollection->addCountToCategories($categories);
return $categories;
}
Created a modified version of app/design/frontend/MYINTERFACE/MYTHEME/template/catalog/navigation/left.phtml to iterate through categories and child categories. Working example at: http://67.228.100.26/content/about