I want to display a title and a content field for each language. So, in the form, I have:
foreach ($languages as $language)
{
// Add the title element
$title = new Zend_Form_Element_Text($language);
$title->setLabel($translate->_('News Title'))
->setBelongsTo('title');
$this->addElement($title);
// Add the content element
$content = new Zend_Form_Element_Textarea($language);
$content->setLabel($translate->_('News Content'))
->setBelongsTo('content');
$this->addElement($content);
}
If I render the form in the usual way it works perfectly:
echo $this->form;
However, I want to render each field separately to include some HTML in the middle and other jQuery stuff. My problem is that I cannot manage to access those elements. I tried
foreach ($languages as $language)
{
$this->form->getElement($language);
}
but it only renders 'content' element. Am I overriding 'title' element?
Thanks
Yes, you're overriding the Title element. The parameter you pass to new Zend_Form_Element_Text($language); ($language in your case) should be unique. Infact you can use it to identify and retrieve the element when you need.
To setup the param you can do something like this:
foreach ($languages as $language)
{
// Add the title element
$title = new Zend_Form_Element_Text('title-' . $language);
...
}
Related
In CakePHP 4.x I'm having some trouble with the mailer() function, specifically passing variables from my controller to a template.
I need a user to be able to receive an email with a list of items. I'm able to use ajax to send an array to my controller, but I can't get the controller to pass the variables to the email template. I'm getting the email but not the dynamic content and I just can't get it to work via setViewVars.
My controller function:
public function bar(): void
{
if( $this->request->is('ajax') ) {
$this->request->allowMethod('ajax');
$foo = $this->request->getQuery('data');
// data is a stringified array
$mailer = new Mailer("default");
$mailer->viewBuilder()->setTemplate('default','default');
$mailer
->setEmailFormat('html')
->setFrom(['email#email.com' => 'Comparaciones'])
->setTo('email#email.com')
->setSubject('About')
>setViewVars($foo)
->send();
// ->deliver() doesn't work either
}
}
My template:
// standard cakephp default template
$content = explode("\n", $content);
foreach ($content as $line) :
echo '<p> ' . $line . "</p>\n";
endforeach;
I'm probably tripping up with something simple, I just can't get the array into the template. Any help much appreciated!!!
I want to wrap h2 tags around a 'content type' text field (the field "product_title"), as a default setting, that the user cannot change.
I have tried to add this snippet in the file template.php in my theme:
function mytheme_preprocess_field(&$vars) {
if($vars['element']['#field_name'] == 'field_produktnamn'){
$vars['items']['0']['#markup'] = '<h2>This works</h2>';
}
}
But I can't figure out how to keep the original field content. I only want to add h2 tags, not replace the text content.
Or maybe I should go about this another way?
try using this instead:
$vars['items']['0']['#prefix'] = '<h2>';
$vars['items']['0']['#suffix'] = '</h2>';
I'm working on the bundle model to check on the quantity of each bundle .. I want to display the error message in array so I can return all the errors at one time. how can I pass array to this line
Mage::helper('bundle')->('msg')? I want to add my errors array instead of message to be like this
Mage::helper('bundle')->($errors). but when I do so it returns array.
if there is also any other solution I'd be grateful :)
if you want to display in forntend site, look like this.
$message = new array();
foreach( $array in $element )
{
...
$message[] = Mage::helper('bundle')->__('msg');
...
}
Mage::throwException(implode(', ', $message));
I have a module, which includes multistep form. I decided to make one of these steps work like this: free times per employee are rendered as html elements, and when element is clicked, Javascript will apply the value to hidden input field.
So I found this tutorial: http://drupal.org/node/1092122 but it didn't work for me, maybe I made a error, maybe not.
My module name is reservation. My template file is named reservation_select_time_week_page.tpl.php and its in the reservation/theme folder.
I have these functions:
function reservation_theme() {
// basic things
$module_path = drupal_get_path('module', 'reservation');
$base = array(
'path' => "$module_path/theme",
);
// define themes
return array(
'reservation_select_time_form' => $base + array(
'render element' => 'form',
'template' => 'reservation_select_time_week_page',
),
);
}
function template_preprocess_reservation_select_time_week_page(&$variables) {
echo "this works!";
}
function template_preprocess_select_time_form(&$variables) {
echo "this works!";
}
I dont know which of those preprocess functions is right (should it be named based on template name or form id?
Anyhow, custom template is not applying.
I also have tried using the:
$form['start_time'] => array('#theme' => 'reservation_select_time_week_page');
I got it working, so that the static html is rendered in template file, but I dont know how I can render the form elements in template file. Using:
drupal_render($form)
caused the form to go infinite loop I think.
Is Drupal even able to handle complex forms and form layouts?
I developed a Content type of "Car Sales" with following fields:
Manufacturer
Model
Make
Fuel Type
Transmission (Manual/Automatic)
Color
Registered? (Yes/No)
Mileage
Engine Power
Condition (New/Reconditioned/Used)
Price
Pictures (Multiple uploads)
I have developed View of this Content Type to display list of cars. Now I want to develop a screen/view for individual Car Sale Record like this:
Apart from arranging fields, please note that I want to embed a Picture Gallery in between. Can this be achieved through Drupal 7 Admin UI or do I need to create custom CSS and template files? If I need to edit certain template files/css, what are those? I'm using Zen Sub Theme.
I would accomplish this by creating a page, and then creating a node template to accompany it. Start by creating a new node, and then record the NID for the name of the template.
Then, in your template, create a new file, and name it in the following manner: node--[node id].tpl.php
Then, in that file, paste in the following helper function (or you can put it in template.php if you're going to use it elsewhere in your site):
/**
* Gets the resulting output of a view as an array of rows,
* each containing the rendered fields of the view
*/
function views_get_rendered_fields($name, $display_id = NULL) {
$args = func_get_args();
array_shift($args); // remove $name
if (count($args)) {
array_shift($args); // remove $display_id
}
$view = views_get_view($name);
if (is_object($view)) {
if (is_array($args)) {
$view->set_arguments($args);
}
if (is_string($display_id)) {
$view->set_display($display_id);
}
else {
$view->init_display();
}
$view->pre_execute();
$view->execute();
$view->render();
//dd($view->style_plugin);
return $view->style_plugin->rendered_fields;
} else {
return array();
}
}
Then add the following code to your template:
<?php
$cars = views_get_rendered_fields('view name', 'default', [...any arguments to be passed to the view]);
foreach ($cars as $car): ?>
<div>Put your mockup in here. It might be helpful to run <?php die('<pre>'.print_r($car, 1).'</pre>'); ?> to see what the $car array looks like.</div>
<?php endforeach;
?>
Just change the placeholders in the code to whatever you want the markup to be, and you should be set!
As I mentioned above, it's always helpful to do <?php die('<pre>'.print_r($car,1).'</pre>'); ?> to have a visual representation of what the array looks like printed.
I use views_get_rendered_fields all the time in my code because it allows me to completely customize the output of the view.
As a Reminder: Always clear your caches every time you create a new template.
Best of luck!