Identifying block variation in PHP render_callback function - php

I am wondering if it is possible to identify which block variation is used within the render_callback function.
Currently I am registering a variation with Javascript, because I found no PHP function doing this:
Javascript:
registerBlockVariation('my/solaredge', {
name: 'CO2',
title: 'SolarEdge CO2',
});
I was in the hopes, that PHP somehow may know what variation it is being rendered.
The closest thing I was able to find was register_block_style(), this one I can identify since get_block_wrapper_attributes() returns the classes string with the style class.

Related

How to use usort on an object with an array of arrays

Simply put: I need to sort an array of objects based on a specific value of a specific array element inside a class property.
This would be great, if it worked:
In this picture, the code illustrates what I want: I want to be able to tell usort to look in a specific array element and find the priority key, and sort based on that integer value.
So, for a given hook (hook-foo), I would need the comparison to be done on:
$plugin->hooks['hook-foo']['priority']
For another hook (hook-bar), for which the same plugin might act upon, I need the sort to look at:
$plugin->hooks['hook-bar']['priority']
if usort supported me sending an arbitrary argument to it, this would be solved. But, the code pictured below was written to illustrate my point and then tested because "maybe it will work". It didn't.
How do I rearrange this to get the sort I need?
(Note: Plugin::hooks is an array of arrays and represents all the metadata required to execute a plugin on a given hook. So, it may contain 'hook-foo' and 'hook-bar', which do not execute at the same time. It may also contain one or the other, or... neither.)
Edit: Here are the example structures, which may help illustrate my point. This shows 2 of 14 plugins loaded.
You can see that each plugin responds to multiple different hooks.
So, if I wanted to control the order for a given hook, I should only have to change the priority of that hook.
For example, here: you can see the cli-init hook fires a method of the class declareMySelf, which simply says: "Plugin XYZ loaded" in the command line interface. Both of the priorities are 50, so it will display them in whatever order the system finds the plugins.
But, if I wanted to display "PHP Mailer loaded" BEFORE I display "Config / Settings management loaded", all I should have to do is drop the PHP Mailer plugin priority to 40.
You should probably have a $hookSearchKey property within your class and set it with a setHookSearchKey() method when needed:
class Whatever {
// ...
private $hookSearchKey
public function setHookSearchKey($key) {
$this->hookSearchKey = $key;
}
//... this is your method of sorting:
public function fooSort(...) {
$this->setHookSearchKey('hook-foo');
usort(...); // do your thing
}
//... this is your method of sorting:
public function barSort(...) {
$this->setHookSearchKey('hook-bar');
usort(...); // do your thing
}
//...
public function sortByHookPriority(...) {
if($a[$this->hookSearchKey] == $b[$this->hookSearchKey]) return 0;
// ...
}
}
Would this work for you?

what is the use of classes_array in drupal7 template.php

in template.php many times they used classes_array..am not getting the meaning and why they using,..what is the purpose of classes_array and when we have to use that in drupal7 .tpl.php
example code:
if(in_array('administrator',array_values($variables['user']->roles)))
{
$variables['classes_array'][]="debug";
}
$variables['classes_array'] is used in preprocess functions. It adds classes to be used in rendering the element to be processed. In your example, a class named "debug" will be added to the html container of the rendered element: if the actual code is
function <YOUR THEME>_preprocess_html(&$variables) {
if (in_array('administrator',array_values($variables['user']->roles))) {
$variables['classes_array'][]="debug";
}
}
your theme will output a body tag like
<body class='debug [...OTHER CLASSES...]'>
for users with administrator role.
You can also add classes to nodes, or other kind of elements for which a preprocess hook is available. E.g. you could write a node preprocess function:
function <YOUR THEME>_preprocess_node($variables) {
$classes_array[] = 'my-class';
}
if you wanted to add 'my-class' to every node of your site.
In general, you will not find $classes_array among the defined variables in tpl.php files. Your theme will, most of the times, implode them in a $classes variable. It must be noted, however, that a kind of confusion arised over time, so different themes may use $classes_array, $attribute_array, $classes, $attributes['class'] and so on for the same purpose, so you should check your theme's documentation to find out what suits your case.

How to set quicksearch initial value?

where I want tu put an initial value.
I have seen that quicksearch has 'q' element but I can't access it, for example this does not find the q element:
$quickSearch->getElement('q');
How can I access the quicksearch in order to set an initial value?
Looking at the source of it can help you find things out. Agile Toolkit is designed in a way where developer should take advantage of the knowledge of the source code.
QuickSearch is derived from Filter which is derived from Form, so there should be addField somewhere. Looking at the QuickSearch, you'll find it inside recallAll() function. There are no calls to this functions so we should look into the parent class - Filter.
Filter sets up a hook in api to call recallAll after initialization have been finished. That means to be able to access the field you can either redefine a method or add a hook yourself.
Hook:
$this->api->addHook('post-init',function() use($quickSearch){
$quickSearch->getElement('q')->set('hello');
});
Extending
class MyQuicksearch extends QuickSearch {
function recallAll(){
parent::recallAll();
$this->getElement('q')->set('hello');
}
}
Finally you can take advantage of knowing where recallAll is loading their default values from and simply do this:
$quicksearch->memorize('q','hello');
to address this, we must first understand how the Search Field of the QuickSearch Class is added to the Grid Basic Class. so upon investigation of the source code, we can see that:
QuickSearch Class does not track (or save a PUBLIC reference of) the Form_Field q
Form_Field q is ONLY added DURING the Rendering phase of the grid
knowing these, we can now proceed adding the modifications to address item #1.
first, we need to add a variable to track the Form_Field q in the QuickSearch Class:
var $search_field=null; // add this line (1)
function recallAll(){
$ff=$this->addField('line','q','');
$this->search_field=$ff; // and this line (2)
parent::recallAll();
:
:
}
second, to address item #2, on our page where the grid is defined, we need add a follow-up hook, example:
class page_gridsearchtest extends Page {
var $search=null;
function init() {
parent::init();
$g = $this->add('MVCGrid');
$g->setModel('Employees');
if($g){
$this->search=$g->addQuickSearch(array('fullname'));
if($this->search)
$this->api->addHook('post-init',array($this,'MyHook')); // add hook
}
}
function MyHook(){ // hooked method
if($this->search->search_field) {
if($this->search->search_field->get()=='')
$this->search->search_field->set('Juan'); // set initial search if blank
$this->search->search_field->setCaption('Employee Name Search');
}
}
}
this will set a CAPTION beside the QuickSearch field and add a DEFAULT search text if the search field is empty.
if this is just a one-time thing, then this may be useful as a quick fix because directly making changes to the library source is very unorthodoxed and does not follow the OOP concept of extending and sub-classing as promoted by ATK.

Expression Engine fetch_action_id() not parsing

So I have tried to get the action ID two ways:
$ACT_ID = $this->EE->functions->fetch_action_id("classname", "function");
$ACT_ID = $FNS->fetch_action_id("classname", "function");
but, it still gives me this as the output:
{AID:classname:function}
and it doesn't parse it when its output into the view. Is there something else I need to do?
For EE2, there are 2 ways of getting an ACT ID, depending on where you're going to use it.
If you're using it in the font-end / templates, use $this->EE->functions->fetch_action_id('class', 'method');, which will return {AID:class:method} in the template, which the template parser in turn will replace with the actual ACT ID. This is done for performance reasons; only 1 query for all ACT IDs is needed. If no valid ACT ID is found, the AID string will remain as is in the template.
If you're using it in the back-end / Control Panel, use $this->EE->cp->fetch_action_id('class', 'method');, which returns the actual ACT ID. The $this->EE->cp object is only available in the Control Panel (for example, the mcp.your_module.php file). If no valid ACT ID is found, it will return FALSE.
There is an undocumented method "insert_action_ids" in the Functions library that parses action_id's in the templates. So from your addon, if you want to simply have the action ID number, you can do the following:
$ACT_ID = $this->EE->functions->insert_action_ids($this->EE->functions->fetch_action_id('classname', 'method'));
Update
I should add that this method will work anywhere - frontend as well as in the CP. But as some have mentioned, when in the templates it is best for performance to use the "insert_action_ids" method and have the template parser replace these with the correct action IDs.

Multiple action contexts in Zend

I am new to Zend and am working on a project that requires three contexts for a particular action. There is the standard context that will normally be used, an AJAX context for AJAX calls, and finally a print-friendly context. The goal is for each of these to have their own view, so the view files used would be something like:
/action_name.phtml
/action_name.ajax.phtml
/action_name.print.phtml
I read http://framework.zend.com/manual/en/zend.controller.actionhelpers.html and came up with:
public function init()
{
// add any necessary context switching here
$contextSwitch = $this->_helper->getHelper('AjaxContext');
$contextSwitch->addActionContext('history', 'html')
->initContext();
//need to add another context for the print view
$this->_helper->getHelper('contextSwitch')->addActionContext('history','print')->initContext();
}
The first two lines I am convinced works, but I am not sure if I am going about the print context the right way, since in the examples the second parameter is normally a file type, like JSON, XML, HTML, etc. Am I going about things the right way or is there something else I should be doing?
It's everything in the documentation. If you want custom contexts, you have to add them first:
$this->_helper
->getHelper('contextSwitch')
->addContext('print', array(
// context options go here
))
->addActionContext('history', 'print')
// more addActionContext()s goes here
->initContext();
What you might do instead of using a context for the print view is just have a parameter in the URL like /print/1. Then in the controller action, check to see if that parameter is true, and if it is, render the "print" view script instead of the regular view script.

Categories