Drupal7 | Custom Module | How to Display Template? - drupal

I'm having trouble figuring out how to display a template that lives inside of my custom module.
This is what I have:
<?php
function brl_footer_theme($existing, $type, $theme, $path) {
$theme = array();
$theme['brl_footer'] = array(
'render element' => 'content',
'template' => 'brl-footer',
'path' => drupal_get_path('module', 'brl_footer'),
);
return $theme;
}
/**
* Implements hook_block_info().
*/
function brl_footer_block_info() {
$blocks = array();
$blocks['brl_footer'] = array(
'info' => t('Custom Footer'),
);
return $blocks;
}
I have a template file in the module called brl-footer.tpl.php
It contains very basic HTML:
<h1>here's some content</h1>
Is it possible to display my template through the custom block 'brl_footer' that's being created?
The custom block is active and has been assigned to the proper region.
Any help on this would be hugely appreciated -

You'll need to implement hook_block_view to define what gets displayed in your block.
Also, if your template is just static content, you don't need to specify a "render element" or "variables" for your theme hook (though you could still make variables in a preprocess function).

Related

Drupal 8 Twig - custom block - two twig templates working third is not?

I have a custom module modero_kbo that creates a custom block.
I need to display this block differently depending on where it is placed on my page.
I have this function in my modero_kbo.module:
function modero_kbo_theme() {
return array(
'modero_kbo_vat' => array(
'variables' => array(
'form' => NULL
)
),
'modero_kbo__landing_page' => array(
'variables' => array(
'form' => NULL
)
),
'modero_kbo__landing_page__modero_kbo_form_2.html.twig' => array(
'variables' => array(
'form' => NULL
)
),
);
}
And this in my custom theme .theme file:
/**
* Implements hook_theme_suggestions_HOOK_alter() for modero_kbo.html.twig.
*/
function moderosolid_theme_suggestions_modero_kbo_vat_alter(array &$suggestions, array $variables) {
if($node = \Drupal::routeMatch()->getParameter('node')){
$suggestions[] = 'modero_kbo__' . $node->bundle();
$suggestions[] = 'modero_kbo__' . $node->bundle() . '__' . $variables['form']['#attributes']['data-drupal-selector'];
}
}
All 3 template suggestions are showing up in my html source on the page.
The first two actually work, the third one is not working.
I've tripple checked all the file names and spelling.
I have 3 different template files, the first two are working, the third one is showing in the suggestions list, but is not used for some reason?
modero-kbo-vat.html.twig
modero-kbo--landing-page.html.twig
modero-kbo--landing-page--modero-kbo-form-2.html.twig
One error we found here is that I should only be using the first array in the modero_kbo_theme() function.
The moderosolid_theme_suggestions_modero_kbo_vat_alter alters that theme.
We could not figure out why the 3rd hook was not working, we suspect that the form variables might not be available at some point in the process.
I solved this by copying the block and creating a new block with a custom template.

Silverstripe 3 - LinkingMode() for Dataobject

I created this function to show dataobjects as page
// DISPLAY ITEM AS PAGE
public function produkt(SS_HTTPRequest $request) {
$urlSegment = $this->request->param('URLSegment');
$item = ShopItem::get()->filter('URLSegment', $urlSegment)->first();
if( $item ) {
$data = array(
'Item' => $item,
'Title' => $item->Title,
'Parent' => Shop::get()->First(),
'Controller' => $this,
'URLSegment' => $item->URLSegment
);
return $this->customise($data)->renderWith(array('ShopItem', 'Page'));
} else {
return $this->httpError(404);
}
}
That's my YML File
---
Name: productRoute
After: 'framework/routes#coreroutes'
---
Director:
rules:
'onlineshop//produkt/$URLSegment!': 'Shop_Controller'
The function is on my Shop_Controller. and the Dataobjects are shown under onlineshop/produkt/blablabla-1
That works fine but in the navigation the link "Onlineshop" is not highlight as section.
I think I need to put the "LinkinMode()" function in my dataobject. But I don't know what the function should contain. return section current or link doesen't work.
Can someone help me?
thank you in advance
You just want to highlight "Onlineshop" which is a Page in your SiteTree, right?
If so, just override the LinkingMode() method inside that Class (extending SiteTree) and make it return section or current for your custom route being active...
You would just need that method on the DataObject itself if you would like to show each DataObject in the Navigation and highlighted when active.
see http://www.ssbits.com/tutorials/2010/dataobjects-as-pages-part-1-keeping-it-simple/

How to display custom html block in Drupal using theme?

Having some module, defined some url in hook_menu() and need to display there some theme (modules/mymodule/templates/mytheme.tpl.php).
How do I show mytheme.tpl.php content on needed url?
function mymodule_menu(){
$item = array();
$item['somemenu'] = array(
'page callback' => 'somemenu_display',
);
return $item;
}
function somemenu_display(){
return WHAT_IS_THIS_FUNCTION('modules/mymodule/templates/mytheme.tpl.php');
}
And it will be good to display only these contents, without and header/footer.
The function is Theme()
return theme('some_theme_function_template', array('aValues' => $someArray));
You then need to use the theme hook like this:
function my_module_name_theme() {
return array(
'some_theme_function_template' => array(
'template' => 'mytheme',
),
);
}
It now searches for mytheme.tpl.php in the root of your module.

simple example form in drupal 7 with everything configured correctly shows "Page not found"..Why..?

I have installed drupal 7 and have been trying to create a custom form. The below code which am trying has been taken from http://drupal.org/node/717722 and I have not made any changes except for .info file.
here is the my_module.info
name = My module
description = Module for form api tutorial
core = 7.x
Below is the my_module.module
<?php
/**
* This function defines the URL to the page created etc.
* See http://api.drupal.org/api/function/hook_menu/6
*/
function my_module_menu() {
$items = array();
$items['my_module/form'] = array(
'title' => t('My form'),
'page callback' => 'my_module_form',
'access arguments' => array('access content'),
'description' => t('My form'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* This function gets called in the browser address bar for:
* "http://yourhost/my_module/form" or
* "http://yourhost/?q=my_module/form". It will generate
* a page with this form on it.
*/
function my_module_form() {
// This form calls the form builder function via the
// drupal_get_form() function which takes the name of this form builder
// function as an argument. It returns the results to display the form.
return drupal_get_form('my_module_my_form');
}
/**
* This function is called the "form builder". It builds the form.
* Notice, it takes one argument, the $form_state
*/
function my_module_my_form($form_state) {
// This is the first form element. It's a textfield with a label, "Name"
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
);
return $form;
}
?>
I have placed these two files in a *my_module* folder and placed it in sites/all/modules
After that, I enabled the module from the modules page without any errors or warnings.
Now, when I try to access this for using the url, localhost/d7/?q=my_module/form
I get a "Page not found " error..!! Why..?? What am I missing..?
Its not only for this module but also for this examples for developers module http://drupal.org/project/examples. It shows the same error.
You should write:
$items['my_module']
Where my_module is module name.
And you need to create page-my_module_my_form.tpl.php file at
sites/all/theme/your_theme/template/page-my_module_my_form.tpl.php
and in this file add code like this:
<?php
if (isset($form['submission_info']) || isset($form['navigation'])) {
print drupal_render($form['navigation']);
print drupal_render($form['submission_info']);
}
print drupal_render($form['submitted']);
?>
<?php print drupal_render_children($form); ?>
and try to run with
localhost/d7/my_module
I hope this will be useful to you
I know this is late, but I do believe that you need to have the $form variable passed into your form, like so : function my_module_my_form($form_state, $form)... That way you actually have a form variable to house your form data.

Suggesting different templates when theming a node form

function posts_theme($existing, $type, $theme, $path) {
return array(
'post_node_form' => array(
'arguments' => array('form' => NULL),
'template' => VARIABLE,
)
);
}
This is the way of suggesting a template to render the 'post_node_form' in Drupal 6. BUT I want to get the node editing form from 2 different paths:
via AJAX through drupal_get_form('post_node_form')
via default node/add/post
If I replace "VARIABLE" depending on the path (or whatever other condition), it will not work because it seems? the name of the template is cached and you need to flush caches to refresh it.
Any solution of suggesting different form templates?
NOTE. This is not the case of node template, (then you can put the template suggestions in the preprocess hooks). It's about node FORM.
Add this function/or modify if exists into template.php of your theme:
function phptemplate_preprocess_page(&$vars) {
// ...
$node = menu_get_object();
if ($node->type == 'post') {
$vars['template_files'][] = VARIABLE;
}
// ...
}
Ok, I answer my own question:
The key of the solution is the hook preprocess_NAME_OF_MY_FORM , that is executed every page load and can be in your module or your theme.
So in my case, I wrote in my "posts" module:
/**
* Implementation of hook_theme().
*/
function posts_theme($existing, $type, $theme, $path) {
return array(
'post_node_form' => array(
'arguments' => array('form' => NULL),
'template' => 'post-form-custom',
)
);
}
function posts_preprocess_post_node_form(&$vars) {
// I check the path to know if node_form is retrieve through normal way or ajax way.
if (check_plain(arg(0)) == 'node'){
$vars['template_files'][] = 'post-form-default';
}
}
I had in my module folder the files post-form-custom.tpl.php and post-form-default.tpl.php

Resources