How to output html from content array property? - php

I'm new to theming and I just inherited this code. I need to control where and how to print #output but it's always showing up at the top of the page, above the HTML tags. I looked at the renderable arrays API but I couldn't find anything specific to my problem.
In mytheme.module:
function mytheme_output_block() {
$content = array(
'#theme' => 'my_theme',
'#output' => function_that_returns_JSON();
...
function mytheme_hook_theme() {
return array(
'my_theme' => array(
'vars' => array(
'output' => '',
...
And in my_theme.tpl.php I tried:
<?php print $output; ?>
But it gets ignored. How do I control #output so I can style it?

not super sure but you might need to call the array element directly. Something like:
$theme = mytheme_hook_theme();
echo $theme['output'];
Hope it helps

Which Drupal you use? I assume D7.
Take a look at the documentation of hook_theme: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_theme/7.x
It's not vars - it's variables
You have missed the template key in your implementation
template: If specified, this theme implementation is a template, and this is the template file without an extension. Do not put .tpl.php on this file; that extension will be added automatically by the default rendering engine (which is PHPTemplate). If 'path', above, is specified, the template should also be in this path.
So what your hook should look like:
function mytheme_hook_theme() {
return array(
'my_theme' => array(
'variables' => array(
'output' => '',
),
'template' => 'my-theme',
...
Remember to clear the caches afterwards.

Related

DRUPAL-7: hook declaration missing

Goodmorning everyone,
I'm new in Drupal.
I hope don't ask a stupid question.
I'm working with Drupal 7 and I need to edit a custom module for my company developed by another developer.
This is a piece of code where I use "theme" function.
This code is under "sites/all/modules/gestione_attivita_attivita/gestione_attivita_attivita.module"
function gestione_attivita_attivita_block_search_attivita($tipo_ricerca) {
$block['subject'] = "";
$ricerca = gestione_attivita_ricerca_fetchAll($tipo_ricerca);
$block['content'] = theme('ricerca_attivita', array(
'items' => $ricerca,
'tipo_ricerca' => $tipo_ricerca
));
return $block;
}
I know that should exist "ricerca_attivita" hook declared somehere in my files.
I'v been looking for something like "['ricerca_attivita'] = array(" or similar words or sub-words in all my files of my site folder but it doesn't exist.
The only thing I know is that under :"sites/all/themes/customModuleOfmyCompany/templates" there are several tpl files and in particular one called "ricerca_attivita.tpl.php" that work and receives data from theme function but I don't know how this is possible.
I don't know who tell to theme call to go on another folder on another path and use "ricerca_attivita.tpl.php" and not foo.tpl.php for example.
Is there anyone that can help me?
Another thing:
Going under includes/theme.inc and debugginng it I have this printing hook info:
array (
'template' => 'ricerca_attivita',
'path' => 'sites/all/themes/customtheme/templates',
'type' => 'theme_engine',
'theme path' => 'sites/all/themes/customtheme',
'preprocess functions' =>
array (
0 => 'template_preprocess',
1 => 'contextual_preprocess',
),
'process functions' =>
array (
0 => 'template_process',
1 => 'ctools_process',
2 => 'rdf_process',
),
)
but I don't know who is that declare it
I think you should use the developer module for themers https://www.drupal.org/project/devel_themer
And about theming function, did you search for this function inside the themes folder?
Hope that helps.

Drupal 7 - node Form template is not used

I have a Problem with one node type and it's form. I want to alter it with an template file. I already did this with another node type on my drupal site and this worked, but it doesn't work for this second type.
So (as I did for the other node type) I placed this hook in my module:
function MY_MODULE_theme($existing, $type, $theme, $path) {
return array(
'FORM_ID' => array(
'arguments' => array(
'form' => NULL,
),
'render element' => 'form',
'template' => 'TYPE-node-form',
'path' => drupal_get_path('module', 'MY_MODULE'),
),
);
}
And also declared this function (in my module):
function template_preprocess_TYPE_node_form(&$variables) {
/* Some hide(elements) and stuff */
}
And of course, I created the file TYPE-node-form.tpl.php in the module directory
/* Something */
TEST
<?php if($form): ?>
<?php print drupal_render_children($form); ?>
<?php endif; ?>
/* Something more */
But it does not load this template ( I cannot see TEST and the other things). Also after multiply times of clearing cache and refreshing.
With DisplaySuite I was able to set a template in the backend (Administration » Structure » Content types » TYPE » Manage fields). But I want to have my own template file (and also not located in the sites/all/modules/ds/layout/.. folder). Deactivating DisplaySuite also does not do the trick.
I also looked tried to place the MY_THEME_theme() code into the template.php file of the administrator theme, but it also did not work.
Any suggestions? What can I do? Is there a way to find out which template is used or where it is overwritten? I have read that Themes overwrite modules templates declaration?!
If you are using D7, hook_theme implementations do not have 'arguments' key, but 'variables'.
Also in this case remove the 'arguments' or 'variables' key in order for the 'render element' to work. Hope this helps.
Final code:
function MY_MODULE_theme($existing, $type, $theme, $path) {
return array(
'FORM_ID' => array(
'render element' => 'form',
'template' => 'TYPE-node-form',
'path' => drupal_get_path('module', 'MY_MODULE'),
),
);
}

File upload in module configuration panel, Prestashop

I'm trying to create a XML import module that will convert given file to CSV format and then use that CSV to import categories and products.
I have a working configuration page made with getContent() it basically calls a method that generates this form via $helper->generateForm(). $helper is a HelperForm() object.
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'type' => 'file',
'label' => $this->l('XML file'),
'name' => 'XMLIMPORT_XML_FILE',
'desc' => $this->l('Select file you wish to import.'),
'required' => true
),
array(
'col' => 3,
'type' => 'text',
'prefix' => '<i class="icon icon-envelope"></i>',
'desc' => $this->l('Enter a valid email address'),
'name' => 'XMLIMPORT_LINES',
'label' => $this->l('Records per file'),
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
I need to get this data to my XML converter. How do I upload a file (around 10-20MB) to Prestashop to be then able to do other stuff with it? How to save it permanently on the server?
I tried doing this:
return array(
'XMLIMPORT_XML_FILE' => Configuration::get('XMLIMPORT_XML_FILE', null),
'XMLIMPORT_LINES' => Configuration::get('XMLIMPORT_LINES', 1000)
);
And after that this:
$form_values = $this->getConfigFormValues(); // returned array from above
foreach (array_keys($form_values) as $key)
Configuration::updateValue($key, Tools::getValue($key));
And later using my own class for XML conversion like this, hoping that it will give me file handle.
$xml_converter = new XMLToCSVConverter(Configuration::get('XMLIMPORT_XML_FILE'), 'output', 'example_products.php');
Apparently it didn't as nothing happens. The class itself is working fine outside of Prestashop module. The constructor is __construct($xml_file, $csv_filename, $template_file).
I need to pass the file I upload to that constructor. I've been struggling for days now.
#edit: I can see the contents of the file inside the HTTP call when submit is clicked. But how do I pass that file to my class?
As far as I remember 'type' => 'file', doesn't actually save any values in the database. This type is only meant to output a file field in your form.
After submitting, you should then do you custom processing with $_FILES['XMLIMPORT_XML_FILE'] : move to upload/ or whereever you want.
'XMLIMPORT_XML_FILE' => Configuration::get('XMLIMPORT_XML_FILE', null), won't return you anything. After uploading you my wish to save the uploaded file path here, but it won't show up next time in the form unless you build the output yourself.
Module configratuon is meant to save text config values. Handling files is trickier and you have to do them yourself each time.
EDIT:
To intercept the saving process, look up the submit button name and make an if statement:
public function getContent() {
if(Tools::isSubmit('submitButtonName')) {
error_log(print_r($_FILES, 1));
}
}
There's probably a function postProcess which does the same (it looks like you copied the methods from a default module).
prestashop handles the image upload with ImageManager class, this class contains more methods which are useful for handling image upload, resize etc.. so its better refer the default homeslider module for the image upload using a module. This module is handling the image upload process in postProcess method with the help of ImageManager class, this class methods will do the all the processes related to upload.

Drupal 7 Node Content Type Template Path

I am working on a module for Drupal 7. I have a template defined for my content type as node--[content type].tpl.php and placed it in the "themes/[selected theme]/template" directory. I want to keep this template in my "module" directory instead. So when the module is installed I don't have to place the file in to the selected theme folder every time. Is there a way to do this?
Thank you all in advance.
I have less than 10 rep so I cant answer my own question so I'll just modify the question
The following is working for me some how. For both case node edit form view and node view
function [content type]_theme() {
return array(
'[content type]_node_form' => array(
'arguments' => array(
'form' => NULL,
),
'template' => 'node--[content type]--edit',
'render element' => 'form',
),
'node__[content type]' => array (
'variables' => array(),
'template' => 'node--[content type]' ,
'base hook' => 'node',
'path' => "sites/all/modules/[content type]_update/[content type]_update/[content type]/",
),
);
}
I don't completely understand this but it's working.
This link should get you going:
http://www.wdtutorials.com/2011/06/13/drupal-7-how-create-custom-theme#.U6sZ741dXag
You basically want to create a new custom theme.
In addition to the above tutorial, create a new folder 'templates' and add your .tpl files in there instead of the core themes folder.
You can use the below format to specify the template path. Then you can place the tpl file in your module folder.
function MODULENAME_theme() {
return array(
'yourcustom_theme' => array(
'template' => 'mypage', // template file called mypage.tpl.php
'path' => drupal_get_path('module', 'MODULENAME'),
)
);
}

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.

Categories