Does anyone know a way to programmatically render a view from a module using the default theme after editing a node?
I'm basically trying to create a static html page of a view.
I have the following code in a custom module:
function MODULENAME_node_update($node) {
unset($node->is_new);
unset($node->original);
entity_get_controller('node')->resetCache(array($node->nid));
$view = views_get_view('references');
$view->set_display('block');
$output = $view->render();
file_put_contents('references.html', $output);
}
The code works but for obvious reasons it renders the view using the admin theme.
I have tried several things to no avail:
variable_set
function MODULENAME_node_update($node) {
variable_set('admin_theme', 'DEFAULT THEME HERE');
[...]
variable_set('admin_theme', 'ADMIN THEME HERE');
}
This hook is probably not the right place to switch themes as it is invoked too late for this.
global $custom_theme
function MODULENAME_node_update($node) {
global $custom_theme;
$custom_theme = 'DEFAULT THEME HERE';
[...]
$custom_theme = 'ADMIN THEME HERE';
}
custom menu item
function MODULE_NAME_menu(){
$items = array();
$items['outputview'] = array(
'title' => 'Test',
'type' => MENU_CALLBACK,
'page callback' => 'MODULE_NAME_output_view',
'access callback' => TRUE,
'theme callback' => 'DEFAULT THEME HERE'
);
return $items;
}
function MODULE_NAME_output_view() {
$view = views_get_view('references');
$view->set_display('block');
$output = $view->render();
file_put_contents('references.html', $output);
}
function MODULE_NAME_node_update($node) {
unset($node->is_new);
unset($node->original);
entity_get_controller('node')->resetCache(array($node->nid));
menu_execute_active_handler('outputview', FALSE); // or via curl
}
This works as the view gets rendered correctly but still uses the admin theme.
hook_custom_theme
function MODULENAME_custom_theme(){
return 'DEFAULT THEME HERE';
}
I am looking for something similar. I found some code doing this (see patch #3 https://drupal.org/node/1813350), but it did not help with our implementation of the Shortcode contrib module. Hopefully it works for you or help you in the right direction.
This is our attempt derived from the patch:
$custom_theme_bu = drupal_static('menu_get_custom_theme');
$custom_theme = &drupal_static('menu_get_custom_theme');
$custom_theme = variable_get('theme_default', 'bartik');
unset($GLOBALS['theme']);
drupal_theme_initialize();
$embed_view = views_embed_view('YOUR_VIEW_ID', 'YOUR_VIEW_DISPLAY_ID');
$custom_theme = $custom_theme_bu;
unset($GLOBALS['theme']);
drupal_theme_initialize();
Here is some self-documenting code based on the answer by lmeurs:
/**
* Switch to or from an alternative theme in the middle of a request.
*
* This is useful if you need to render something (like a node) in a different
* theme without changing the theme of the entire page. An example use case is
* when you need to render something for a front end user from an admin page.
*
* Usage example:
* my_module_switch_theme('bartik');
* $node = node_load(1);
* $renderable = node_view($node);
* $rendered = render($renderable);
* my_module_switch_theme();
*
* #param string|null $to
* The name of the theme to switch to. If NULL, it switches back to the
* original theme.
*/
function my_module_switch_theme(string $to = NULL) {
// Backup the original theme.
static $original_theme;
if (empty($original_theme)) {
$original_theme = drupal_static('menu_get_custom_theme');
}
// Get a reference to the current theme value.
$custom_theme = &drupal_static('menu_get_custom_theme');
// Perform the switch.
$custom_theme = $to ?? $original_theme;
unset($GLOBALS['theme']);
drupal_theme_initialize();
}
Related
How to change the node preview title to be displayed instead of just "Preview"?
This is a way to solve your problem in your custom module
/**
* implements hook_form_BASE_FORM_ID_alter
* the form id that will build the node preview is page_node_form
* #param $form
* #param $form_state
*/
function yourmodulename_form_page_node_form_alter( $form, $form_state ){
if( !empty( $form_state['node']->in_preview ) ){
// security hint: do not pass the PASS_THROUGH param to the drupal_set_title
// because the node title may contain some xss. Without this parameter the
// drupal_set_title will check for xss and remove them if present
drupal_set_title(t('Preview') . ' ' . $form['#node']->title );
}
}
Here how to hack core to modify the title (quick and easy way) but better would be to override via a custom module (perhaps somebody else can post please).
In /modules/node/node.pages.inc add $node->title within drupal_set_title(t('Preview'), PASS_THROUGH);
like so:
// Display a preview of the node.
if (!form_get_errors()) {
$cloned_node->in_preview = TRUE;
$output = theme('node_preview', array('node' => $cloned_node));
unset($cloned_node->in_preview);
}
drupal_set_title(t('Preview: '.$node->title), PASS_THROUGH);
I want build a form using a block module in Drupal 8. I am aware of building the forms in Drupal 7 but the same seems to be different in Drupal 8.
Request anyone who has worked on drupal8 custom forms as block to help me.
Your question is very vague, as I don't know how much you already know about modules, forms and blocks in Drupal 8. So here is a small guide what to do, further information on how to do stuff in detail would be overkill for this answer.
1. Create a new module and enable it
Look here: Naming and placing your Drupal 8 module.
Basically you create the module folder and the module info yml file to let Drupal know about the module. Then you enable it using drush or the admin area in Drupal.
2. Create the form
Look here: Introduction to Form API.
under your_module/src/Form you create the form. More details in the link above.
3. Create the block and render the form
Look here: Create a custom block.
under your_module/src/Plugin/Block/ you create the block which will render the form.
The idea is basically (code updated with suggestion from Henrik):
$builtForm = \Drupal::formBuilder()->getForm('Drupal\your_module\Form\YourForm');
$renderArray['form'] = $builtForm;
return $renderArray;
Note: You don't need to wrap the $builtForm with the $renderArray, you can return just the $builtForm and be fine. I just personally like to do it that way, because often times I need to add something else to the final render array like some markup, cache settings or a library etc.
4. Place the block
Place the block in the desired region(s). Done.
To build a form using block module, you can easily use Webform module where you can add a form and display as a block.
If you mean to create a form programatically in the custom block, you can achieve that by creating two files shown below:
Form file (src/Form/DemoForm.php):
<?php
/**
* #file
* Contains \Drupal\demo\Form\DemoForm.
*/
namespace Drupal\demo\Form;
use Drupal\Core\Form\FormBase;
class DemoForm extends FormBase {
/**
* {#inheritdoc}.
*/
public function getFormId() {
return 'demo_form';
}
/**
* {#inheritdoc}.
*/
public function buildForm(array $form, array &$form_state) {
$form['email'] = array(
'#type' => 'email',
'#title' => $this->t('Your .com email address.')
);
$form['show'] = array(
'#type' => 'submit',
'#value' => $this->t('Submit'),
);
return $form;
}
/**
* {#inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
$values = $form_state->getValues();
if (strpos($values['email'], '.com') === FALSE ) {
$form_state->setErrorByName('email', t('This is not a .com email address.'));
}
}
/**
* {#inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
drupal_set_message($this->t('Your email address is #email', array('#email' => $form_state['values']['email'])));
}
}
Source: Building a Drupal 8 Module: Blocks and Forms.
Block file (src/Plugin/Block/HelloBlock.php):
<?php
namespace Drupal\mymodule\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'Hello' Block.
*
* #Block(
* id = "form_block",
* admin_label = #Translation("My form"),
* category = #Translation("My Category"),
* )
*/
class HelloBlock extends BlockBase {
/**
* {#inheritdoc}
*/
public function build() {
$form = \Drupal::formBuilder()->getForm('\Drupal\mymodule\Form\HelloBlock');
//$form['#attached']['js'][] = drupal_get_path('module', 'example') . '/js/example.js';
//$form['#markup'] = $this->t('Custom text');
return $form;
}
}
Source: Create a custom block.
To add a form to the Block Configuration, see: Add a Form to the Block Configuration.
Here is a detailed summary of how to go about this:-
https://www.sitepoint.com/building-drupal-8-module-blocks-forms/
Following the above guide, you would add the completed form to the block build function, e.g.
class DemoBlock extends BlockBase {
/**
* {#inheritdoc}
*/
public function build() {
$form = \Drupal::formBuilder()->getForm('Drupal\demo\Form\DemoForm');
return $form;
}
}
Some more useful docs if you are new to Drupal 8 or need to dust off your knowledge:
https://www.drupal.org/docs/8/creating-custom-modules
https://www.drupal.org/docs/8/api/block-api
https://www.drupal.org/docs/8/api/form-api
I have correctly created a custom widget, evreything is translating well with a correct .po file, except the title.
Here is my code :
$concert_widget_name = __('Tour Dates', 'concerts');
wp_register_sidebar_widget (
'tourdates', // your unique widget id
$concert_widget_name, // widget name
'tourdates_widget_display', // callback function to display widget
array( // options
'description' => 'Displaying upcoming tour dates'
)
);
Is there an error ? An other way to translate the widget name ?
I usually register my widgets using the register_widget function. In the constructor of the widget class I place the following code:
class TourDates extends WP_Widget
{
public function __construct()
{
$options = array('classname' => 'tour-dates', 'description' => __('Display upcoming tour dates'));
parent::__construct('tour_dates', __('Tour Dates'), $options);
}
}
You can also check out the Widgets API on the WordPress Codex site. Hopefully this helps you in creating your custom widget.
Also what I usually do is merge my translations with the default ones loaded from WordPress, like so:
function loadTextDomain() {
$locale = get_locale();
$languageDir = dirname(__FILE__) . '/languages';
$domain = 'default';
$mofile = $languageDir . '/theme.' . $locale . '.mo';
global $l10n;
$mo = new MO();
if (!$mo->import_from_file($mofile)) {
return false;
}
if (isset($l10n[$domain]) && !empty($l10n[$domain]->entries)) {
$l10n[$domain]->merge_with($mo);
} else {
$l10n[$domain] = $mo;
}
}
add_action('init', 'loadTextDomain');
This code looks similar to the load_textdomain function from WordPress but it avoids all the filters that do exist in the original function, which helps in avoiding any WordPress hook from altering your $domain and $mofile variables.
But I will leave that up to you. Maybe the load_textdomain() function from WordPress will work just as fine, but in case it doesn't this function should do the trick.
Now if your using the loadTextDomain() function I pasted above you can just place a languages folder in the same folder as your functions.php resides, in that new folder you could place theme.nl_NL.mo or theme.de_DE.mo files depending on the language your using. This should allow translation of your website and also the admin area.
I'm writing a custom Drupal 7 module which will completely override the search page and search method for a website. Here is what I have so far:
/**
* Custom search.
*/
function mymodule_search_page() {
drupal_add_css('css/search.css');
// Perform a search (not important how)
$result = do_custom_search('foo');
return '<p>Results:</p>';
}
Now, as you can see, it's not complete. I don't know how to properly return structured HTML from this. How would I go about using Drupal's built-in template system to render the results?
you have to make use of drupal inbuilt functions. i hope you are looking for something like this http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_render/7
This is what I ended up doing:
/**
* Implements hook_menu().
*/
function mymodule_search_menu() {
$items = array();
$items['search'] = array('page callback' => 'mymodule_search_page',
'access callback' => TRUE);
return $items;
}
/**
* Mymodule search page callback.
*/
function mymodule_search_page() {
$variables = array();
// Add stuff to $variables. This is the "context" of the file,
// e.g. if you add "foo" => "bar", variable $foo will have value
// "bar".
...
// This works together with `mymodule_search_theme'.
return theme('mymodule_search_foo', $variables);
}
/**
* Idea stolen from: http://api.drupal.org/comment/26824#comment-26824
*
* This will use the template file custompage.tpl.php in the same
* directory as this file.
*/
function mymodule_search_theme() {
return array ('mymodule_search_foo' =>
array('template' => 'custompage',
'arguments' => array()));
}
Hope this helps someone!
I really only need the mlid and title text for the first level below a certain menu item. Here's what I'm doing at the moment. (It works, but I suspect there may be a more drupal-y way.):
/**
* Get all the children menu items below 'Style Guide' and put them in this format:
* $menu_items[mlid] = 'menu-title'
* #return array
*/
function mymod_get_menu_items() {
$tree = menu_tree_all_data('primary-links');
$branches = $tree['49952 Parent Item 579']['below']; // had to dig for that ugly key
$menu_items = array();
foreach ($branches as $menu_item) {
$menu_items[$menu_item['link']['mlid']] = $menu_item['link']['title'];
}
return $menu_items;
}
Is there?
Actually there is an easy way to get that information by using menu_build_tree():
// Set $path to the internal Drupal path of the parent or
// to NULL for the current path
$path = 'node/123';
$parent = menu_link_get_preferred($path);
$parameters = array(
'active_trail' => array($parent['plid']),
'only_active_trail' => FALSE,
'min_depth' => $parent['depth']+1,
'max_depth' => $parent['depth']+1,
'conditions' => array('plid' => $parent['mlid']),
);
$children = menu_build_tree($parent['menu_name'], $parameters);
$children contains all information you need. menu_build_tree() checks access or translation related restrictions too so you only get what the user really should see.
afaik, there isn't (i hope i am wrong).
for the while, instead of digging for ugly keys, you can turn your function into a more abstract helper function by simply adding a foreach ($tree). then you can use your own logic to output what you want (mlid, in this case). here is my suggestion:
/**
* Get the children of a menu item in a given menu.
*
* #param string $title
* The title of the parent menu item.
* #param string $menu
* The internal menu name.
*
* #return array
* The children of the given parent.
*/
function MY_MODULE_submenu_tree_all_data($title, $menu = 'primary-links') {
$tree = menu_tree_all_data($menu);
foreach ($tree as $branch) {
if ($branch['link']['title'] == $title) {
return $branch['below'];
}
}
return array();
}
Have you looked into the Menu block module? Some more details about this module (from its project page):
... have you ever used the Main and Secondary menu links feature on your theme and wondered “how the hell do I display any menu items deeper than that?”
Well, that’s what this module does. It provides configurable blocks of menu trees starting with any level of any menu. And more!
So if you’re only using your theme’s Main menu links feature, you can add and configure a “Main menu (levels 2+)” block. That block would appear once you were on one of the Main menu’s pages and would show the menu tree for the 2nd level (and deeper) of your Main menu and would expand as you traversed down the tree. You can also limit the depth of the menu’s tree (e.g. “Main menu (levels 2-3)”) and/or expand all the child sub-menus (e.g. “Main menu (expanded levels 2+)”).
I use this :
Just add your path and eventualy the menu and it will give you the child.
function MY_MODULE_submenu_tree_all_data($path, $menu = 'main-menu', $curr_level = 0, $rebuilt_path='', $childtree = array()) {
$tree = menu_tree_all_data($menu);
$args = explode('/', $path);
$rebuilt_path = empty($rebuilt_path) ? $args[$curr_level] : $rebuilt_path . '/' . $args[$curr_level];
foreach ($tree as $branch) {
if ($branch['link']['link_path'] == $rebuilt_path) {
$childtree = $branch['below'];
if ($rebuilt_path != $path) {
$curr_level++;
MY_MODULE_submenu_tree_all_data($path, $menu, $curr_level, $rebuilt_path, $childtree);
}
}
}
$items = array();
foreach ($childtree as $child) {
$items[] = l($child['link']['title'], $child['link']['link_path']);
}
return theme('item_list', array('items' => $items, 'attributes' => array(), 'type' => 'ul'));
}
Here's a helper function to return a whole subtree of a menu, starting at a specified mlid. Some of the other posts only return the direct descendants of the current item; this will return ALL descendants.
By default it gives you the subtree starting with the current page, but you can pass in any menu tree (as returned by menu_build_tree) and any mlid.
function _menu_build_subtree($menu=NULL,$mlid=NULL) {
if ($menu == NULL || $mlid == NULL) {
$parent = menu_link_get_preferred();
}
$menu = !is_null($menu) ? $menu : menu_build_tree($parent['menu_name']);
$mlid = !is_null($mlid) ? $mlid : $parent['mlid'];
foreach ($menu as $branch) {
if ($branch['link']['mlid'] == $mlid) {
return $branch;
}
$twig = _menu_build_subtree($branch['below'],$mlid);
if ($twig) { return $twig; }
}
return array();
}