I want to add inline style to YII t(' ') - php

I want to add style ' float:right ' for this text. I tried some way,but not works. How should i add inline css for this.
public function title()
{
return $this->t('You have {amount} of credit',
array('{amount}' => m('payment')->format(wm()->get('payment.helper')->credit())),
array('style'=>'float:right'));
}
Can you give me a hint, please?

Yii translation function looks like this: Yii::t('category', 'message', \['params' => 'value'\]). So you need style to be in your message to translate it.
Yii::t(
'admin',
'You have <span style="{style}">{amount}</span>',
[
'{style}' => 'color: red;',
'{amount}' => 'Test',
]
);
-> <span style="color: red;">Test</span>

Agree with #Justinas, (Great thanks for your example, it really works) but there one tiny mistake in your code:
'{style}' => 'color: red;' // in this case style would not be applicable to a span
has to be so:
'style' => 'color: red;'
Tested on my case, works for sure.

Related

Highlight search result in cakephp

as the title says I need to highlight the input string of a search in the results, I mean if I look for "ome" the results should look like this:
c ome
s ome
Jer ome
s ome one
etc.
Searching on the documentation I have found that I need to activate the "Text" helper and that I should have a code similar to this:
echo $this->Text->highlight($string);
Even though I've tried many possibilities, I can not make it work, I know "Text" helper is working because this works for me at View/Users/index.ctp:
echo $this->Text->autoLinkEmails(h($user['User']['email']));
if it helps, this is the code I've generated for the search which works correctly:
file: Model/Oldcaterm.php
public $actsAs = array(
'Search.Searchable'
);
public $filterArgs = array(
'entry' => array(
'type' => 'like',
'field' => array('entry', 'lemma_tag')
),
'lemma_tag' => array(
'type' => 'like',
'field' => array('entry', 'lemma_tag')
),
);
file: Controller/OldcatermsController.php
public function index() {
$this->Oldcaterm->recursive = 0;
$this->Prg->commonProcess();
$this->Paginator->settings['conditions'] = $this->Oldcaterm->parseCriteria($this->Prg->parsedParams());
$this->set('terms', $this->Paginator->paginate());
}
file: View/Oldcaterms/index.ctp
<?php
echo $this->Form->create(null,array());
echo $this->Form->input('entry', array('label' => 'Cerca coincidència'));
echo $this->Form->end(__('Submit'));
?>
I have read and try many differents ways, but I'm not able to make it work, can somebody please guide me a little, I have found next example at the documentation:
// called as TextHelper
echo $this->Text->highlight(
$lastSentence,
'using',
array('format' => '<span class="highlight">\1</span>')
);
// called as String
App::uses('String', 'Utility');
echo String::highlight(
$lastSentence,
'using',
array('format' => '<span class="highlight">\1</span>')
);
But I don't find the way to make it fit into my code
Thanks in advance.
After a few studies, I realized the css style is not there. Right click on your browser and explore the source code, if it shows <span class="highlight">Your Text Here</span> then everything is just right. Just add followings code to your CSS file
.highlight{
background-color: #FFDF00; /* <-- Choose what colour you want to use. Here is Golden Yellow */
}
If you still have any problem, please share in the comment.

JavaScript into Drupal Module

I'm trying to implement a JavaScript script into a Drupal 7 module and for some reason I keep getting this error here: "Parse error: syntax error, unexpected T_ECHO, expecting ')'"
I've been attempting this for a few hours now, but I'm just at a loss. Any help would be so greatly appreciated.
My code block indented to the far right. The other code is part of a module in Drupal 7 for your reference.
$node->content['actions'] = array(
'#theme' => 'links',
'#prefix' => '<div id="match-actions">',
'#suffix' => '</div>',
'#links' => _match_actions($node),
echo '<script type="text/javascript">'
, 'croll();'
, 'troll();'
, '</script>';
'#attributes' => array(
'class' => array('links', 'match-actions'),
),
'#heading' => t('Match actions'),
'#weight' => -10,
);
The JavaScript I'm trying to insert (as you can see my attempt in the echo above) is
function class_roll() {
// Car Classes
var classes = ["B", "A", "S", "R3", "R2", "R1"],
classToUse = classes[Math.floor(Math.random() * classes.length)];
document.getElementById("croll").innerHTML = classToUse ;
}
function track_roll() {
var tracks = ["Clear Springs Circuit", "Festival North Circuit", "Beaumont Circuit", "Finley Dam Circuit", "Gladstone Circuit", "Clifton Valley Trail", "Gladstone Trail", "Red Rock Descent", "Red Rock Hill Climb"],
trackToUse = tracks[Math.floor(Math.random() * tracks.length)];
document.getElementById("troll").innerHTML = trackToUse ;
}
What exactly am I doing wrong? I've been searching around Stack and all over the net which has enabled me to try different syntaxes, but I just can't get it to work. I'm not an expert in JS and PHP, but I'm trying to learn :). Again, any help is REALLY appreciated.
P.S. - In HTML terms, what I'm trying to do is this:
<p id="croll">Some text here</p>
<p id="troll">Some text here</p>
<button onclick="class_roll(); track_roll();">Class Roll</button>
but it would be great if instead of doing an onclick type of PHP action, it would be an onload type of event, but it would only onload for the first time and stick there and remain static.
You can't put an echo inside of an array.
You should just be able to do:
$links = _match_actions($node);
$links[] = '<script type="text/javascript"> croll(); troll(); </script>'
$node->content['actions'] = array(
'#theme' => 'links',
'#prefix' => '<div id="match-actions">',
'#suffix' => '</div>',
'#links' => $links,
'#attributes' => array(
'class' => array('links', 'match-actions'),
),
'#heading' => t('Match actions'),
'#weight' => -10,
);
As HorusKol said you cannot directly call a JavaScript function inside your module. The reason being modules are written in PHP, and one cannot call functions of other programming language into one.
If you wish to insert JavaScript code, you should use the function drupal_add_js() to do so.
So, you could replace your echo with the following:
echo drupal_add_js('croll();troll();','inline');
The better method, that won't break with things like Drupal AJAX, is to use Drupal behaviors.
(function ($) {
Drupal.behaviors.myModuleName = {
attach : function (context, settings) {
$(".match-actions", context).once('match-actions', function(){
croll();
troll();
})
}
}
})(jQuery);
Place that in a js file and load it with drupal_add_js(drupal_get_path('module', '{my module name}') . '{js file name}');

Replace text link with image using php

l(t('Edit'), 'node/' . $node->nid . '/webform/components/' . $cid, array('query' => drupal_get_destination())),
I'd like to be able to replace the word "Edit" with an image instead, however when I put an image src in there it shows up as text. Is there a different function I should be using?
I'm using Drupal as my framework.
Thanks ffrom a noob.
Best way to do this is to combine l() and theme_image():
print l(theme_image(array('path' => 'edit_button.png', 'attributes' => array('title' => t('Edit')))), 'node/' . $node->nid . '/webform/components/' . $cid, array('query' => drupal_get_destination(), 'html' => TRUE));
You should also use drupal_get_path() to prefix the path of your image.
This way you will fully apply to the Drupal Coding standards, and make your life easier.
That button should have its own class, for example, action_button or an id such as edit_button. Now use CSS to hide the text and use a background image in its place as shown below:
.action_button, #edit_button {
/* Hide Text */
text-indent: -9999px;
display: block;
background: url('edit_button.png') no-repeat;
/* Set width and height equal to the size of your image */
width: 20px;
height: 20px;
}
EDIT 1:
Change your code with below and use above CSS:
l(t('Edit'), 'node/'.$node->nid.'/webform/components/'.$cid, array('attributes' => array('id' => array('edit_button')), 'query' => drupal_get_destination()));
EDIT 2 [FINAL]:
Passing html as TRUE to array parameter of l() method can make us able to use first parameter as img tag instead of just text for display as link:
l('<img src="edit_button.png" alt="Edit" />', 'node/'.$node->nid.'/webform/components/'.$cid, array('query' => drupal_get_destination(), 'html' => 'TRUE'));
This is the best way I implemented:
list($nodeImage) = field_get_items('node', $node, 'uc_product_image');
$style_array = array('path' => $nodeImage['uri'], 'style_name' => 'MY_IMAGE_STYLE');
$render_node_image = theme('image_style', $style_array);
$render_node_image_WITH_LINK = l($render_node_image, 'node/' . $node->nid, array('html' => TRUE));
print $render_node_image_WITH_LINK

Specify Image Height and Width in php string

I have a linked image that I need to specify height and width html attributes for that is generated by php string. I've looked into using getimagesize but its not looking like what I need. I am currently using social engine.
This is the string I have that calls the image link.
<?php echo $this->htmlLink($item->getHref(), $this->itemPhoto($item, 'null'), array('class' => '')) ?>
which outputs html somthing like <img src="#" class="null"/>
I need to modify the php to create an image that is specified to be 110px x 110px
ie <img src="#" class="null" width="110" height="110"/>
Can anyone give me an example of how I could write my string?
You have to use this:
<?php echo $this->htmlLink(
$item->getHref(),
$this->itemPhoto(
$item,
'null',
null,
array('width' => '110px', 'height' => '110px')),
array('class' => '')
) ?>
That is, add a fourth parameter array to itemPhoto function.
Socialengine is an extension of Zend framework. You should try to check out the definition of itemPhoto, htmlLink etc. functions in order to customize them. For example itemPhoto can be found here- \application\modules\Core\View\Helper\ItemPhoto.php
The last parameter is used for that, isn't that?
$this->htmlLink(
$item->getHref(),
$this->itemPhoto($item, '', '', array('width' => 110, 'height' => 110)),
array('class' => '')
);
Method1:
Just add style for null class
<?php echo $this->htmlLink($item->getHref(), $this->itemPhoto($item, 'null'), array('class' => '')) ?>
as:
.null {
height: 110px;
width: 110px;
}
Method2:
Or you can pass a inline style attribute as:
<?php echo $this->htmlLink($item->getHref(), $this->itemPhoto($item, 'null'), array('class' => '', 'style' => 'height: 110px; width: 110px')) ?>
I cant see whats behind the function htmlLink() but I guess due to the name of those elements you pass you could just write.
echo '<img src="'.$this->itemPhoto($item, 'null').'" class="null" width="110" height="110"/>';
but ether post that function or just try and error!

Add text/html behind a form element?

How can I add a text behind a form element in Zend Framework? Let's say I have a text input field, first there is a label, bellow the label there is an actual input field but I want to add some additional text (2-3 sentences) behind the input field. Something like a short tip or suggestion what is the best value for that field.
You can simply add a description to the input element, e.g.
$element = new Zend_Form_Element_Text('itemName', array(
'label' => 'My Label',
'description' => 'Enter some text'
));
From the ZF Manual Chapter 23.7.3. Zend_Form_Decorator_Description:
By default, if no description is present, no output is generated. If the description is present, then it is wrapped in an HTML p tag by default, though you may specify a tag by passing a tag option when creating the decorator, or calling setTag(). You may additionally specify a class for the tag using the class option or by calling setClass(); by default, the class 'hint' is used.
So to modify the decorator you would write something like
$element->setDecorators(
array(
'ViewHelper',
'Errors',
array('Description', array('placement' => 'append',
'escape' => false,
'tag' => 'span')),
array('HtmlTag', array('tag' => 'dd')),
array('Label', array('tag' => 'dt', 'escape' => false))
));
You also might want to check this series of tutorials about Zend Form Decorators.
You can use the description option as an element option to add a <p> tag with your description, after the element (with default decorators.)
You strategy then simply requires some CSS to position the description in the right place. Something like
dd.myelement{
position: relative;
}
dd.myelement p {
position: absolute;
top:0;
right: 0;
}
You'll probably need z-index properties on the input and p too:
dd.myelement p {
z-index: 1;
}
dd.myelement input {
z-index: 2;
}
There might be a CSS property missing (I've written that from memory) but I hope you get the idea :)
Richard,
you need a decorator! :) They are sometimes a little complicated, but I've been using the following tutorial, which is probably a little outdated, but not less helpful. Especially check the use of legend. I think that's perfect for what you want to do.
Hope this helps!

Categories