CakePHP extend currency formatting - php

I need to extend the number helper to include other currencies. Using the "addFormat" function in the number helper, I have created a new "CurrenciesHelper" to add these currencies.
<?php
class CurrenciesHelper extends NumberHelper {
I know I need to make it possible for this helper to run this function immediately. What am I missing to contain this so it runs and adds this format?
$this->Number->addFormat('CAD', array(
'before'=>'$',
'after' => false,
'zero' => 0,
'places' => 2,
'thousands' => '.',
'decimals' => ',',
'negative' => '()',
}

Starting CakePHP 2.1, you don't need to extend the helper to get this done. NumberHelper has been re-factored into CakeNumber class. If you go through the code, you can see that the formats are now stored as a static array.
That helps us configure currency formats within app/Config/core.php, like the following:
App::uses( 'CakeNumber', 'Utility' );
CakeNumber::addFormat(
'CAD',
array(
'before' => '$ ', 'after' => false,
'zero' => 0, 'places' => 2, 'thousands' => '.',
'decimals' => ',', 'negative' => '()', 'escape' => true
)
);
// ... and any more definitions to follow.
Once your currency definitions are part of the core configuration, you can use them in any view using NumberHelper like $this->Number->currency( $c, 'CAD' ).

PREVIOUS ANSWER CHANGED: Due to position of functions, after the fact. Had to change to:
function __beforeRender(){
}
Was causing overwriting of other custom currencies.
By encapsulating it in a construct
function, it run on helper
construction. I was sure to include
the parent constructor just in case.
function __construct() {
parent::__construct();
// code goes here
}

Related

Extending page table with field using itemsProcFunc in TYPO3 6.2

I'm trying to extend the 'page' properties form by extending the page db table and TCA array from within an extension. This works, except that my custom function won't be called. If I replace my own itemsProcFunc line with a TYPO3 core function itemsProcFunc line it works, but with my own function it never works (I just get an empty result/selectlist, even when I simply return a dummy array: "return array('title','1');"....
Here's my code in my extension's ext_tables.php:
<?php
$TCA['pages']['columns'] += array(
'targetelement' => array(
'exclude' => 0,
'label' => 'Target element (first select a target page!)',
'config' => array (
'type' => 'select',
'items' => Array (
Array('',0),
),
'size' => 1,
'minitems' => 1,
'maxitems' => 1,
//'itemsProcFunc' => 'TYPO3\CMS\Backend\View\BackendLayoutView->addBackendLayoutItems',
'itemsProcFunc' => 'Vendor\Myextension\Controller\Hooks\CustomTargetElementSelector->getContentElements',
),
)
);
t3lib_extMgm::addToAllTCAtypes('pages', 'targetelement,', '2', 'after:nav_title');
t3lib_extMgm::addToAllTCAtypes('pages', 'targetelement', '1,5,4,199,254', 'after:title');
P.s. I replace Vendor\Myextension for my own namespace of course.
I don't know where to put my function file exactly, I assume in extension\Classes\Controllers\Hooks\CustomTargetElementSelector.php.
My ultimate goal is to display a list of content elements of the selected shortcut page UID..
P.s.2 my CustomTargetElementSelect.php file looks like this (contents just return a single item, dummy list result:
<?php
namespace Vendor\Myextension\Controller;
class CustomTargetElementsSelector extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
public function getContentElements(array &$params,$pObj){
return array('title','uid');
}
}
First of all, an itemsProcFunc should be a simple class; I never tested if an Extbase controller context is available in an itemsProcFunc.
Your hook should (this is just a recommendation) reside in
yourext/Classes/Hook/CustomTargetElementSelector.php
Namespace:
namespace Vendor\Yourext\Hook;
class CustomTargetElementSelector {
[method inside]
}
After flushing the system cache, if the hook still has no function, set a die() statement within the function to find out if the function is called at all. Currently it cannot work because the location of your class (Controllers/Hooks) and the namespace (Controller) don't fit.
For the sake of full 6.2/7 compatibility, replace
t3lib_extMgm::
by
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::

Drupal : Create variable from custom .module to custom .tpl.php

I create a custom module and I want use the complete_url of my future website in the template who used/created by my module.
I tried several ways and searched Google but I don't find a solution.
However this issue seems very simply, that became me crazy x)
So, I must create a variable in my .module and add her when I return the array in the main_socialtag_theme function. Where/How can I do that ?
My .module:
function main_socialtag_block_info(){
$block['main_socialtag']=array(
'info' => t('Main socialtag'),
'cache' => DRUPAL_NO_CACHE,
);
return $block;
}
function main_socialtag_theme(){
return array(
'main_socialtag_block' => array(
'template' => 'theme/main_socialtag_block',
'variables' => array(),
),
);
}
function main_socialtag_block_view($delta=''){
if ($delta == 'main_socialtag'){
return array(
'subject' => '',
'content' => array(
'#theme' => 'main_socialtag_block',
)
);
}
}
Thanks for help, and sorry for my bad english writing !
If you are looking for a way to add, modify and call variables. Check variable_get and variable_set.
To add or modify a variable value:
variable_set("my_variable_name", "value");
To retrieve the value:
$myVal = variable_get("my_variable_name", "");
For hook_theme Implementation, kindly check this question.

Override Theme-Options.php file

I am having a difficult time removing the calling from my parents theme-options.php. My problem is this: My parent theme calls for the logo not from the header.php which I could easily replace but from the theme-options.php by placing the url path to the image like so require ( get_template_directory() . '/settings/theme-options.php' );
I want to "unrequire" this call in the child theme and add a function to call require ( get_stylesheet_directory() . '/theme-options.php' ); This way I can modify the options file and remove the call to the url. I have had a similar problem before and fixed it by using the unregister function but I am not sure on how to "unrequire" this call to the file.
Any help would be greatly appreciated.
After searching a little deeper there is a function that calls the default values like so :
function max_magazine_default_options() {
$options = array(
'logo_url' => get_template_directory_uri().'/images/logo.png',
'favicon_url' => '',
'rss_url' => '',
'show_slider' => 1,
'slider_category' => 0,
'show_carousel' => 1,
'carousel_category'=> 0,
'show_feat_cats' => 1,
'feat_cat1'=> 0,
'feat_cat2'=> 0,
'feat_cat3'=> 0,
'feat_cat4'=> 0,
'show_posts_list' => 1,
'show_author' => 1,
'show_page_comments' => 1,
'show_media_comments' => 1,
'ad468' => '<a href='.get_site_url().'><img src='.get_template_directory_uri().'/images/ad468.png /></a>',
'inline_css' => '',
'meta_desc' => '',
'stats_tracker' => '',
'google_verification' => '',
'bing_verification' => '',
);
return $options;
}
So im guessing I could remove this function and replace it with my own and alter the values?
The only way you would be able to do this without editing the parent is if they call require() within a function called by a hook such as init, setup_theme, etc. Then you could simply do:
remove_action( 'hook', 'their_function_name' );
And then you would have to copy/paste the function into your functions.php file, edit it, and then re-add it using the same hook:
add_action( 'hook', 'your_function_name' );
You may also have to make sure the priority (third argument) is correct on your function to make sure it properly overrides it.

Drupal module: create a text format

I've created a module which defines a new text format filter.
Now I want to define a text format using this new filter, directly from module php. Drupal Administrator can do this manually from admin/config/content/formats/add admin page, but I want to avoid this step. What do I need to add to my_dmodule.module?
I'm guessing that you are trying to create a text format on module installation. If so, you could call filter_format_save(). The details on creating the object can be found in filter.module (Drupal API reference).
You might have to load the filter module first if you are creating the filter in hook_install(), haven't checked:
drupal_load('module', 'filter');
Inspired by Hendrik's answer, this is my solution:
function myformat_install() {
drupal_load('module', 'filter');
/* check already exists */
$format_exists = (bool) db_query_range('SELECT 1 FROM {filter_format} WHERE name = :name', 0, 1, array(':name' => 'My Format'))->fetchField();
if (!$format_exists) {
$format = array(
'format' => 'myformat',
'name' => 'My Format',
'filters' => array(
'myformat_filter' => array(
'weight' => 0,
'status' => 1,
),
),
);
$format = (object) $format;
filter_format_save($format);
}
}
myformat_filter is a filter defined implementing hook_filter_info(), but it could be a filter defined in another module.

Zendframework2 Dependency Injection Confusion

I'm a little confused on how DI works with ZF2. I've spent the last couple of days trying to get my head around it. While I have made some progress a lot of it still baffles me...
Using this (http://akrabat.com/getting-started-with-zend-framework-2/) tutorial I managed to get a grasp that the following:
'di' => array('instance' => array(
'alias' => array(
'album' => 'Album\Controller\AlbumController',
),
'Album\Controller\AlbumController' => array(
'parameters' => array(
'albums' => 'Album\Model\Albums',
),
),
works because in our Album Controller class we have a setAlbum function. So when the DI class will call that setAlbums function and pass it the 'Album\Model\Albums' class.
Fine get that no problem..
Now let's look at this (which comes in the skeleton application off the zend site)
'Zend\View\HelperLoader' => array(
'parameters' => array(
'map' => array(
'url' => 'Application\View\Helper\Url',
),
),
),
Now i would expect there within the Zend\View\HelperLoader (or an inherited class) would contain a setMap() function that the DI class would pass an array. But this appears not to be the case. As I cannot find a setMap anywhere.
My question is first what am I not understanding about the way DI works with the ZF2... But also what does the code above (about zend\view\helper) actually do. I mean what does injecting 'map' => array('url' => 'Application\View\Helper\Url') into the Zend\View\HelperLoader actually do?
Thanks for any help anyone can give. I appreciate it's a beta framework and what answers I may get now not apply in a months time. But this all seems pretty fundamental and i'm just no getting it!
The DI configuration of ZF2 works indeed with the names of the arguments in the signature. It does not matter if this is done with a constructor or a explicit setter. The setter must, however, start with "set" to be recognized by Zend\Di\Di.
So if you have a class like this:
<?php
namespace Foo;
class Bar
{
public function __construct ($baz) {}
public function setSomethingElse ($bat) {}
}
You can inject both a $baz and a $bat:
'di' => array(
'instance' => array(
'Foo\Bar' => array(
'parameters' => array(
'baz' => 'Something\Here',
'bat' => 'Something\There',
),
),
),
)
For Zend\Di it does not matter what the function name exactly is, as long as it starts with "set" and the name of the argument is correct. That is why Foo\Bar::setSomethingElse($bat) works just like Foo\Bar::setBat($bat).
Just make sure you name your arguments correctly. For example, it is easy to do something like this:
<?php
namespace Foo;
class Bar
{
public function setCacheForBar ($cache) {}
public function setCacheForBaz ($cache) {}
}
But that will not work nicely together with Zend\Di.

Categories