Widget Elementor that adds a Div to body using hook - wordpress

Is there a way to add a div to body when the Widget is rendered? Like using a hook 'wp_body_open' or something.
Where can I put a hook in Widget_Base extended class for it?
I'm using a common Class like:
use Elementor\\Widget_Base;
use Elementor\\Controls_Manager;
class MenuHamburguer extends Widget_Base
{
public function __construct($data = [], $args = null)
{
parent::__construct($data, $args);
// I've tried here, but no success.
}
public function get_name()
{
return 'menu-hamburguer';
}
public function get_title()
{
return __('Menu Hamburguer', 'later');
}
public function get_icon()
{
return 'fa fa-bars';
}
public function get_categories()
{
return ['later'];
}
protected function _register_controls()
{
// do normal stuff of the widget...
}
protected function render()
{
// do normal stuff of the widget...
}
}

Related

How to fix Avoid unused parameters PHPMD of protected method in parent class

I have two class :
class EntityVoter
{
protected function canPutToBlockChain($entity, $viewer)
{
return false;
}
}
class VerificationVoter extends EntityVoter
{
public function canPutToBlockChain($entity, $viewer)
{
return $this->canShare($entity, $viewer);
}
}
PHPMD scan EntityVoter class and throw: Avoid unused parameters such as '$entity', '$viewer'.
My solution is creating a interface:
interface EntityVoterInterface
{
function canPutToBlockChain($entity, $viewer);
}
and then add #inhericDoc annotation:
class EntityVoter implements EntityVoterInterface
{
/**
* #inheritDoc
*/
protected function canPutToBlockChain($entity, $viewer)
{
return false;
}
}
Is there any better solution ?

SilverStripe: How to set/ specify the icon for GridField custom action button

I am working on a SilverStripe project. In my project, I am trying to create a GridField custom button. I followed the official documentation. Following is the class form the SilverStripe official page.
class GridFieldCustomAction implements GridField_ColumnProvider, GridField_ActionProvider, GridField_ActionMenuItem
{
public function getTitle($gridField, $record, $columnName)
{
return 'Custom action';
}
public function getCustomAction($gridField, $record)
{
if (!$record->canEdit()) {
return;
}
return GridField_FormAction::create(
$gridField,
'CustomAction'.$record->ID,
'Custom action',
"docustomaction",
['RecordID' => $record->ID]
)->addExtraClass(
'action-menu--handled'
);
}
public function getExtraData($gridField, $record, $columnName)
{
$field = $this->getCustomAction($gridField, $record);
if (!$field) {
return;
}
return $field->getAttributes();
}
public function getGroup($gridField, $record, $columnName)
{
return GridField_ActionMenuItem::DEFAULT_GROUP;
}
public function augmentColumns($gridField, &$columns)
{
if (!in_array('Actions', $columns)) {
$columns[] = 'Actions';
}
}
public function getColumnAttributes($gridField, $record, $columnName)
{
return ['class' => 'grid-field__col-compact'];
}
public function getColumnMetadata($gridField, $columnName)
{
if ($columnName === 'Actions') {
return ['title' => ''];
}
}
public function getColumnsHandled($gridField)
{
return ['Actions'];
}
public function getColumnContent($gridField, $record, $columnName)
{
$field = $this->getCustomAction($gridField, $record);
if (!$field) {
return;
}
return $field->Field();
}
public function getActions($gridField)
{
return ['docustomaction'];
}
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
{
if ($actionName !== 'docustomaction') {
return;
}
// perform your action here
// output a success message to the user
Controller::curr()->getResponse()->setStatusCode(
200,
'Do Custom Action Done.'
);
}
}
I am struggling to add the custom icon for the button or specify the style class of the button. I can change the column class name. But I cannot find a way for the button. How can I do that?
you can achieve this using ->setAttribute('classNames', 'font-icon-<your-icon>');
i.e. for "edit" icon the code would looks like this:
return GridField_FormAction::create(
$gridField,
'CustomAction'.$record->ID,
'Custom action',
"docustomaction",
[
'RecordID' => $record->ID
]
)
->addExtraClass('action-menu--handled')
->setAttribute('classNames', 'font-icon-edit');
You can find all available icons on this page:
https://gbaumeister.github.io/ss4-icons/

PHPUnit Mocking Wrapper Instances

I have this wrapper:
class APIWrapper {
protected static $clientClass = \Hardcoded_API; // from a library
protected $client;
public function __construct() {
$this->client = new self::$clientClass(array_merge(self::$config, $config));
}
/**
* Does something.
*
* #throws SomeException
*/
public function act() {
$this->client->doSomething();
}
public static function setClientClass($clientClass) {
self::$clientClass = $clientClass;
}
}
Now let's say I am testing functions that make use of this wrapper:
class UnrelatedFunction {
public function callAPI() {
$wrapper = new APIWrapper();
try {
$wrapper->act();
} catch (SomeException $e) {
// ... do other things
}
}
}
class UnrelatedFunctionsTest extends PHPUnit_Framework_TestCase {
public function testUnrelatedFunctionException() {
$unrelatedFunction = new UnrelatedFunction();
$this->setExpectedException('SomeException');
// this call triggers the APIWrapper
$unrelatedFunction->callAPI();
// Some assertions here...
}
}
I need to be able to verify that UnrelatedFunction::callAPI() did its job recovering from the exception. In this case, how can I mock a \Hardcoded_API class, make it throw a SomeException when act() is called, and pass it onto APIWrapper::setClientClass() so that I can test the behaviour?

Setting body classes in Zend

I am working on a Zend 1.12 application and trying to get style classes assigned to layout body tag.
I found this sample Bootstrap.php file that seems to handle the task:
https://gist.github.com/fideloper/1302688
It seems to integrate nicely, but the body's class always comes out blank.
Can someone please point me in the right direction here on how to get classes assigned to body?
Thanks.
Made a small change to the snippet I referenced above.
class AppName_Helper_BodyClass extends Zend_View_Helper_Placeholder_Container_Standalone {
private $_classes = array();
public function __construct($classes = null) {
if(is_array($classes)) {
$this->addClass($classes);
}
}
public function addClass($class) {
if(is_array($class)) {
foreach($class as $k => $c) {
if(is_string($c)) {
if(is_string($k)) {
$this->addClass($k.'-'.$c); //recursion
} else {
$this->addClass($c);
}
} else {
throw new Zend_Exception('Class must be a string - is type: '.gettype($c));
}
}
return $this;
}
if(is_string($class)) {
$this->_classes[] = $class;
return $this;
} else {
throw new Zend_Exception('Class must be a string - is type: '.gettype($class));
}
return $this;
}
public function removeClass($class) {
$key = array_search($class, $this->_classes);
if($key !== false) {
unset($this->_classes[$key]);
}
return $this;
}
public function bodyClass() {
return $this;
}
public function toString() {
return implode(' ', $this->_classes);
}
}
This snippet goes into my layout:
$uri = Zend_Controller_Front::getInstance()->getRequest()->getParams();
$this->bodyClass()->addClass($uri);
The results is such (for module - "default", controller - "auth", action - "signin"):
<body class="controller-auth action-signin module-default">
I am grabbing current request's parameters and mapping them to the body class. Hope this helps someone dealing with this.
From the look of that helper you need to call one of it's methods to add a class. From one of your controllers:
$this->view->bodyClass()->addClass('something');
is that how you are using it?

Symfony2 + Twig: Translate label into a new twig extension

I have implemented a new twig extension and I have some text which had to be translated.
Unfortunately when I use a code label it appears as a sample text.
I mean when twig render this following extension, it displays: 5 entity.years instead of 5 years for example:
class MyExtension extends \Twig_Extension {
public function getFilters()
{
return array(
'myextension' => new \Twig_Filter_Method($this, 'myextension'),
);
}
public function myextension ($myId)
{
// ....
// Some operations concerning $myId...
// ....
if($myId!=0) {
$res = $myId. ' '.'entity.year';
} else {
$res = ($months == 0 ? $days.'entity.days' : $months.'entity.months');
}
return $res;
}
}
Where entity.years, entity.months, entity.days is defined into my translations folder.
Inject the translator service into your extension and use it. For example:
class MyExtension extends \Twig_Extension
{
private $translator;
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
// ...
public function myMethod()
{
return $this->translator->trans('my_string');
}
}

Resources