Zend forms: How to surround an image-element with a hyperlink? - image

How do I surround an image element inside a Zend form with an HTML <a href>-Tag? I specifically don't want a JS onClick()-Event as it seems not to work properly in my case (see question Zend: Redirect from form without validation).
This is how I add the image element inside the form, right now the hyperlink is displayed as Text "Link" right next to the image:
$this->addElement('image', 'btnBack', array (
'name' => 'btnBack',
'id' => 'btnBack',
'label' => '',
'title' => 'Go back',
'alt' => 'Go back',
'src' => '/img/undo.png'
));
$this->getElement('btnBack')
->setDescription('Link')
->setDecorators(array(
'ViewHelper',
array('Description', array('escape' => false, 'tag' => false)),
array('HtmlTag', array('tag' => 'dd')),
array('Label', array('tag' => 'dt')),
'Errors',
));

This sounds like a bad idea. From the look of your other question, all you want is an image 'button' that goes to a specific URL. You shouldn't try and do this with an image input, since this is a type of submit button, and so clicking it will submit the form. If you're trying to override this with javascript or wrap the whole thing with a HTML link you're trying to solve the wrong problem.
The easiest approach is probably to use a button input. You can then style it with CSS to make it look like an image, and add your javascript onclick action to make it do what you want. Since input type="button" elements don't do anything onclick by default, you shouldn't have any of the problems you had before.
$this->addElement('button', 'btnBack', array(
'id' => 'btnBack',
'label' => 'Go back',
'onclick' => "window.location='/admin/groupoverview'"
));
then in your stylesheet (replace the width and height with the correct image dimensions):
#btnBack {
background-image: url(/img/undo.png);
width: 123px;
height: 123px;
border: 0;
text-indent: -1000px;
cursor: pointer;
}
the text-indent shifts the text label out of view. If you absolutely don't want these rules in your stylesheet you could put them inline on the form element by passing a style attribute instead.

Related

How to get tinybutstrong mergeblock with condition working for empty value

I am using tinybutstrong as a templating engine.
I am trying to get the active link to display in a different color.
The following is my (trimmed down) PHP side:
$links = array (
'' => 'Home',
'about' => 'About Us',
'faq' => 'FAQ',
'contact' => 'Contact Us',
);
$uri = substr($_SERVER['REQUEST_URI'], 1);
$TBS->MergeBlock('blkLinks', 'array', $links);
Html Template:
<ul>
<li>[blkLinks.val;]</li>
</ul>
While it generates the html fine and nicely changes the color of the links when that page is currently active, this does not work for the empty (/) link.
How can I fix that?
Simply replace the condition
[onload.uri]=[blkLinks.$]
with
'[onload.uri]'='[blkLinks.$]'
The point is that when [onload.uri] is an emprty string, then the expression can be ambiguous. Using the string delimitors, the expression is clear for TBS.

Drupal ajax form, partial render

Good day.
I created custom form in my module and defined submit button like this:
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#ajax' => array(
'callback' => 'fmg_addbanner_ajax_callback',
'method' => 'replace',
'wrapper' => 'banner_add_wrapper'
)
);
Then outputted my form like this:
$form = drupal_get_form('fmg_banner_add_form', $region_id);
print render($form);
But ajax requests don't work. I think because of there are no needed drupal js files. How can I solve this problem? Thanks.
The normal case will be to have #ajax attached to a common input, like checkbox, text box etc.
The behavior is that when user change the input value, it'll fire your callback through ajax and send entire form over, and then you can process this form behind the scene and adjust certain things before sending the form back and change the element to the wrapper you defined.
One of the question that I had for partial form rendering is when I need to do some ajax call or inject partial form element to certain area of the page while keeping the integrity of the form rendering. (I don't know exactly what form build did, i guess i don't want to know)
Since form rendering uses the renderable array, if you just want to extract certain element, you could just do
$form = drupal_get_form('application_form');
$body = render($form['body']);
The following is a real example that i took a form and split into header, footer and the rest of form elements.
// get a form for updating application info.
$form = drupal_get_form('pgh_awards_review_application_form', $app);
$elements = array();
$form_render = render($form);
$elements = array(
'qualify' => render($form['qualify']),
'threshold_met' => render($form['threshold_met']),
'submit' => render($form['submit']),
);
if (preg_match('/<form.+div>/', $form_render, $matches)) {
$elements['header'] = $matches[0];
}
if (preg_match('/<input type="hidden".+form>/s', $form_render, $matches)) {
$elements['footer'] = $matches[0];
}
$vars['form'] = $elements;
It's definitely not pretty approach, it just serves the needs at the moment.

Why wysiwyg plugin in magento gets me broken links?

When I try to insert an image to the wysiwyg editor in Magento it places the right variable of the image path in the media directory.
But, wysigyg doesn't find the right path/url, example:
I inserted:
<p><img src="{{media url="wysiwyg/blog/Screen_Shot_2013.png"}}" alt="" /></p>
But, in the preview it gets me the next broken src:
<img src="https://localhost/index.php/admin/cms_wysiwyg/directive/___directive/e3ttZWRpYSB1cmw9Ii93eXNpd3lnL2Jsb2cvU2NyZWVuX1Nob3RfMjAxMy5wbmcifX0,/key/1a966015551bf00867319d23c9914b48/" alt="" data-mce-src="https://localhost/index.php/admin/cms_wysiwyg/directive/___directive/e3ttZWRpYSB1cmw9Ii93eXNpd3lnL2Jsb2cvU2NyZWVuX1Nob3RfMjAxMy5wbmcifX0,/key/1a966015551bf00867319d23c9914b48/">
And of course, it doesn't show the image. Also this happens with links and other media files.
System -> Configuration -> General -> Content Management -> Use Static URLs for Media Content in WYSIWYG for Catalog to Yes
The option wich Le Nguyen gave is not really a good solution.
Like hsanders said:
There are some pretty big drawbacks to doing this: The static URL will be a non-secure URL and If you change your base URL the URLs on the images will not update to reflect that are the two I can think of immediately.
A possible solution to "fix" the problem (not the right way in my opinion, but at least better than the static Url option) would be to add some code to the following file:
vendor/magento/module-catalog/Ui/DataProvider/Product/Form/Modifier/Eav.php
Add on line 747:
'add_variables' => false,
'add_widgets' => false,
'add_directives' => true,
Also change
lib/web/mage/adminhtml/wysiwyg/tiny_mce/setup.js and add the following on line 384 (below content = editor.getContent();:
content = this.decodeContent(content);
Now you need to execute the following command to complete the process:
php bin/magento cache:flush
Hope this helps.
(Keep in mind you've just edited core files)
in your Form.php you configuration should be like below
protected function _prepareForm()
{
$model = Mage::registry('testimonial_data');
$form = new Varien_Data_Form();
$this->setForm($form);
$form->setHtmlIdPrefix('testimonial_');
$wysiwygConfig = Mage::getSingleton('cms/wysiwyg_config')->getConfig(array('add_variables' => false, 'add_widgets' => false,'files_browser_window_url'=>$this->getBaseUrl().'admin/cms_wysiwyg_images/index/'));
$fieldset->addField('content', 'editor', array(
'name' => 'content',
'label' => Mage::helper('testimonials')->__('Content'),
'title' => Mage::helper('testimonials')->__('Content'),
'style' => 'width:700px; height:500px;',
'state' => 'html',
'config' => $wysiwygConfig,
'required' => true,
));
}
hope it will work for you.

How to use ajax on a field widget submit button in Drupal 7 without validating the rest of the form

I'm making a new widget for taxonomy term references where a submit button makes an ajax call back to drupal to alter the form. Here's what I have:
$element['my_module_wrapper']['add'] = array(
'#type' => 'submit',
'#value' => t('Add'),
'#ajax' => array(
'callback' => 'my_module_ajax',
'wrapper' => $field_name . '_my_module_container',
),
);
I have the ajax call working properly, but it validates the whole form, and throws errors when unrelated fields that are required aren't filled out. How can I stop this?
Also possibly related, it doesn't seem to call my hook_field_widget_form() function when ajax is called... is this stemming from the same problem?
Have you tried making your field #type = 'button' instead of 'submit'? As far as I know, a submit button will always run through the for validate array whether it's an ajax form item or not.
Look at the example from Poll module given here: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7#ajax
The full function shows how it's done with '#limit_validation_errors'.

Cakephp 1.3, Weird behavior on firefox when using $this->Html->link

Greetings,
I am getting a very weird and unpredictable result in firefox when using the following syntax:
$this->Html->link($this->Html->div('p-cpt',$project['Project']['name']) . $this->Html->div('p-img',$this->Html->image('/img/projects/'.$project['Project']['slug'].'/project.thumb.jpg', array('alt'=>$project['Project']['name'],'width'=>100,'height'=>380))),array('controller' => 'projects', 'action' => 'view', $project['Project']['slug']),array('title' => $project['Project']['name'], 'escape' => false),false);
OK I know it is big but bear with me.
The point is to get the following output:
<a href="x" title="x">
<div class="p-ctp">Name</div>
<div class="p-img"><img src="z width="y" height="a" alt="d" /></div>
</a>
I'm not sure if this validates correctly both on cakephp and html but it works everywhere else apart from firefox.
You can actually see the result here: http://www.gnomonconstructions.com/projects/browser
To reproduce the result use the form with different categories and press search. At some point it will happen!!
Although most of the time it renders the way it should, sometimes it produces an invalid output like that:
<div class="p-cpt">
name
</div>
<div class="p-img">
<img src="x" width="x" height="x" alt="x" />
</div>
Looks like it repeats the link inside each element.
To be honest the only reason I used this syntax was because cakephp encourages it.
Any help will be much appreciated :)
I am guessing that the name of some projects is null. According to the documentation, if you pass null as the second argument to the div() method, it will not generate the closing tag (and the resulting markup will be invalid).
The example of invalid markup that you pasted above appear to have come from Firebug rather than Page Source. Use Page Source to view the actual markup sent to the browser. The anchor tag is not repeated.
I rewrote your code to better see what happens. Copy it into one of your views, change 'My Project' to null, and notice how it will affect the $name_div variable:
<div class="p-cpt">My Project</div> will turn into <div class="p-cpt">.
<?php
$project['Project']['name'] = 'My Project';
$project['Project']['slug'] = 'my-project';
$image = $this->Html->image(
'/img/projects/' . $project['Project']['slug'] . '/project.thumb.jpg',
array(
'alt' => $project['Project']['name'],
'width' => 100,
'height' => 380
)
);
$name_div = $this->Html->div('p-cpt', $project['Project']['name']);
$image_div = $this->Html->div('p-img', $image);
$link = $this->Html->link(
$name_div . $image_div,
array(
'controller' => 'projects',
'action' => 'view',
$project['Project']['slug']
),
array(
'title' => $project['Project']['name'],
'escape' => false
)
);
var_dump($image);
echo 'Notice what happens below if project name is null.';
var_dump($name_div);
var_dump($image_div);
var_dump($link);
echo $link;

Resources