Drupal 7 how to apply custom template to my custom form - drupal-7

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?

Related

Yii2 Fileinput widget initializing after renderAjax

I am completely lost for two days and have no clue how to fix this nonsense!
I use fileinput widget in my page like this:
echo FileInput::widget([
'name' => 'attachment_product[]',
'id' => 'img_products',
'options' => [
'multiple' => true
],
'pluginOptions' => $pluginOptions,
]);
In the same page i have a button that when clicked, reloads the content with renderAjax.
On success i reload the content like this:
$('.content').html(data);
The problem is that the fileInput widget is not being initialized.. I dont know what to do and i get this error:
fileinput_516a5cfe is not defined
As i read here: https://github.com/kartik-v/yii2-widgets/issues/229 and here: https://github.com/kartik-v/yii2-widget-switchinput/issues/14. I need to reinitialize the plugin but I have no clue how to do that... I am lost..
This is by design, A JavaScript file would typically only need loading once so all subsequent requests filter out already included files.
If you want to load it with every request you can add it's path to yii.reloadableScripts yii.js
https://github.com/yiisoft/yii2/issues/10251

How to strip HTML from a field in drupal views

I am trying to add a function to that strips the html from a field in drupal views. I found a function for sql server called "udf_StripHTML" that does that.
http://blog.sqlauthority.com/2007/06/16/sql-server-udf-user-defined-function-to-strip-html-parse-html-no-regular-expression/
I am using the following code:
/**
* Implements hook_views_query_alter().
*/
function cviews_views_query_alter(&$view, &$query) {
// Add a strip html tags from content.
$fields = array('field_data_body.body_value');
foreach ($query->where as $key1 => $value) {
foreach ($value['conditions'] as $key2 => $coditions) {
if (in_array($coditions['field'], $fields)) {
$query->where[$key1]['conditions'][$key2]['field'] = 'dbo.udf_StripHTML(' . $coditions['field'] . ')';
}
}
}
}
When views module converts the query object to a string the field become
from:
'dbo.udf_StripHTML(field_data_body.body_value)';
to:
[dbo].[udf_StripHTMLfield_data_body.body_value]
My question is, how can I add a function there?
Thank you,
You are going way too deep here friend. I'm going to assume that you're using Drupal 7, but for Drupal 8 this should be similar (since views is in core for both).
A few things about your approach:
That function is a user defined function which means that it needs to be defined at a much lower-level (in the SQL database) before you can use it in your query.
This is a red-herring approach, however, because you don't need to even touch the SQL to accomplish what you want (you can do this with PHP with strip_tags!)
You don't need a query alter hook here (we don't need to go to the database to do this). You could do this with one of the preprocess or field hooks from the field API or the views API using the function linked in my previous point.
Even better, you don't even have to touch the code to accomplish this. You can do it right in the Drupal UI.
Under the field settings for the view, select rewrite results and then Strip HTML tags. Presto, no more HTML tags in that field.
Image source: https://www.drupal.org/node/750172
Here is the solution that worked for me:
// Traverse through the 'where' part of the query.
foreach ($query->where as &$condition_group) {
foreach ($condition_group['conditions'] as &$condition) {
if (in_array($condition['field'], $fields)) {
$value = $condition['value'];
$field = $condition['field'];
$condition = array(
'value' => array(),
'field' => t('dbo.udf_StripHTML(!field) like \'#value\'', array(
'!field' => $field,
'#value' => $value)),
'operator' => 'formula',);
}
}
}

CakePHP 2.x display map location using address?

I have CakePHP website I want to display Google map and marker in given address on my CakePHP application.
Is there any plugin available to display address in Google map.
You probably did not even try to google.
Or you would have stumpled upon at least a dozen plugins/solutions.
Check this one out:
http://www.dereuromark.de/2010/12/21/googlemapsv3-cakephp-helper/
// include jquery js
$this->Html->script('jquery', array('inline' => false));
// include the google js code
$this->Html->script($this->GoogleMapV3->apiUrl(), array('inline' => false));
// echo the div container to display the map in
echo $this->GoogleMapV3->map(array('div'=>array('height'=>'400', 'width'=>'100%')));
$options = array(
'lat' => 48.95145,
'lng' => 11.6981,
'title' => 'Some title', // optional
'content' => '<b>HTML</b> Content for the Bubble/InfoWindow' // optional
);
$this->GoogleMapV3->addMarker($options);
// finalize js
$this->GoogleMapV3->finalize();
// Make sure you got `echo $this->Js->writeBuffer(array('inline' => true));` somewhere in your layout then
If you do not have the lat/lng yet, you can either geocode via JS at runtime or use Cake to do so: http://www.dereuromark.de/2012/06/12/geocoding-with-cakephp/
You can also use CakePHP-GoogleMapHelper that will geocode the address on the fly and add it to your map. You would do something like this: (see more details on the link shared)
<?= $this->GoogleMap->addMarker("map_canvas", 1, "1 Infinite Loop, Cupertino, California"); ?>

how do i include a tpl file in my module drupal 7

I'm building a module using hook_preprocess_node()
I've made a new view mode for the node entity called ´vacancy_teaser´ using the hook_entity_info_alter()
this shows up in my node display settings and view
so I want to use the template included in my module when this view mode is used.
my code:
/**
* Implements hook_preprocess_node().
*/
function vacancies_preprocess_node(&$vars) {
if($vars['view_mode'] == 'vacancy_teaser') {
$vars['theme_hook_suggestions'][] = 'node_vacancy_teaser';
}
}
my template file is called: ´node-vacancy-teaser.tpl.php´ but is not used in the output of my view
$vars['view_mode'] == 'vacancy_teaser' in the view. ( tested )
but where does $vars['theme_hook_suggestions'][] = 'node_vacancy_teaser'; looks for the template file? somehow it's not included / used.
apparently in drupal 7 useing dubble underscores is required for some reason.
node_vacatures_vacancy_teaser.tpl.php placed in the active template folder seems to do the trick... although I don't think this is a neat solution since the tpl.php file is separated from the module.
Be sure to specify the template file in the hook_theme implementation. The examples project is great for finding out the details of how to do things like this. Specifically, check out the theming_example_theme() function in the theming_example module…
function theming_example_theme() {
return array(
// …
'theming_example_text_form' => array(
'render element' => 'form',
// In this one the rendering will be done by a tpl.php file instead of
// being rendered by a function, so we specify a template.
'template' => 'theming-example-text-form',
),
);
}
Instead of appending to the end of the $vars['theme_hook_suggestions'] array, try:
array_unshift($vars['theme_hook_suggestions'], 'node_vacancy_teaser');
This will pass your suggestion to the front of the array, and it will be found first. Most likely since you are appending it to the end of the array, Drupal is finding an existing theme suggestion first and using it instead (such as node.tpl.php).

Show same form many times on one page (D7+formapi)

I'm trying show on views (each row) my custom form. It works, but the form doesn't work proper. I need to give to each form different ids.
hook_forms should help me, but I cannot manage how to use it. If I put print $form into it, I still cannot see my forms.
I'm calling my form like this from the template file:
$form1 = drupal_get_form('votingform_create_decision_form_' . $node->nid, $node->nid);
print drupal_render($form1);
Now I got it working:
function hook_forms($form_id, $args) {
if ($args[0] == 'mymodule_form_') {
$forms['mymodule_form_' . $args[1]] = array(
'callback' => 'mymodule_form',
'callback arguments' => array($args[1]),
);
}
return $forms;
}

Resources