Drupal, how to Remove Login or comment link from webform pages? - drupal

I want to remove the Login or register to post comments text from the page where I created a webform; is there any suggestion as to how can i use hook_link_alter() with this?

This code resides in comment.module file under the theme_comment_post_forbidden() function.
If you are using Drupal 7, you may use hook_node_view_alter or hook_entity_view_alter to modify the displayed content.
function foo_node_view_alter (&$build) {
if ($build['#node']->type == 'webform') {
// remove login or register to post comments
unset($build['links']['comment']['#links']['comment_forbidden']);
// remove add comments
unset($build['links']['comment']['#links']['comment_add']);
}
}
In case you want to use hook_link_alter in Drupal 6, use this code in your custom module
function comment_link_alter (&$links, $node) {
if ($node->type == 'webform') {
// remove register or login to post comments
unset($links['comment_forbidden']);
// remove add a comment
unset($links['comment_add']);
}
}

If you are working with a content type, you could over-ride the theme.
copy the template file '/modules/node/node.tpl.php' into your theme's 'templates' directory
rename the file, calling it 'node--NODETYPE-tpl.php' (that's two hyphens after 'node'). For example, 'node--book-tpl.php' for a 'book' content type.
comment out the final two lines (or delete them):
// print render($content['links']);
// print render($content['comments']);

Related

How I can print all variable of a hook in drupal 8?

I'm very new in Drupal 8 and I have issue now with hook. Mainly I though that I don't clearly understand structure and hook definition in Drupal 8.
So my main problem is that I have some hook to interact with main menu (add custom class name to ul, li and link, a tag). I can do it by changing template file and now try to do it with any hook.
Although I found that some hook relating to menu ex. hook_contextual_links_alter (link: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Menu%21menu.api.php/function/hook_contextual_links_alter/8.9.x).
At the end of this hook we have the code related:
function hook_contextual_links_alter(array &$links, $group, array $route_parameters) {
if ($group == 'menu') {
// Dynamically use the menu name for the title of the menu_edit contextual
// link.
$menu = \Drupal::entityTypeManager()
->getStorage('menu')
->load($route_parameters['menu']);
$links['menu_edit']['title'] = t('Edit menu: #label', [
'#label' => $menu
->label(),
]);
}
}
So I have installed devel module with kint function and in my .theme file and try:
function hook_contextual_links_alter(array &$links, $group, array $route_parameters) {
kint($links);
}
and then reload my Home page but nothing showed. But I can get some information about other like:
function eg_learn_theme_suggestions_page_alter(&$suggestions, $variables) {
kint($suggestions);
}
So what happens here? Can you help to explain if how I can print the variable of this hook (in .theme file) and the site page to see the printing variable?
In general when I found a hook, how I can print there array and check it in website?
There are some problems about your approach:
When implementing a hook, you must replace "hook" with the module name/theme name where you put the hook function inside. For example, if you want implement hook_contextual_links_alter in your_custom module, it becomes your_custom_contextual_links_alter().
Not all hook can be implemented in the theme. Some hook can only be implemented in modules (in .module file). You can read more here.
In your case, I think hook_preprocess_menu would be more suitable. You can implement it in your custom theme like this:
function <your_theme_name>_preprocess_menu(&$variables) {
if ($variables['menu_name'] == 'main') {
kint($variables);
}
}

How do I alter a node template from a module in Drupal 8?

I would like my custom Drupal 8 module to force a different template for pages that match a certain URL. Here is my module structure:
/sass_edit
/css
/js
/template
builder_template.html.twig // this is the template file
sass_edit.module
...
sass_edit.*.yml // various yml files
These are the contents of builder_template.html.twig:
<h1>test</h1>
Here are the relevant lines of code in my .module file:
function sass_edit_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
$current_path = \Drupal::service('path.current')->getPath();
if(\Drupal::currentUser()->isAuthenticated() && strpos($current_path, "/builder/") >= 0) {
$suggestions[] = 'builder_template';
}
// kint($suggestions);
// die();
}
When I visit a page whose URL contains /builder/, the code runs and adds the string builder_template to the $suggestions array. However, when the page is rendered, the new template is ignored. I have tried flushing caches, with no results.
I'm not sure how to proceed. All the documentation I've found refers to adding theme suggestions from a custom theme, not a custom module.
Any idea what I'm doing wrong? Thank you so much for any help!
Maybe you have a typo in the code you pasted but the folder where you have your templates overrides should be templates and not template as you wrote. Don't forget to flush caches after changing the name of the directory ;)

How can I redirect a Drupal user after they create new content

I want to redirect my users after they create a piece of new content, instead of directing them to the 'view' page for the content.
I have seen mention of using hook_alter or something, but I'm really new to drupal and not even sure what that means?
Thanks!
As you mention that you are new to Drupal, I'd suggest to take a look at the Rules module. You can add a trigger on for content has been saved/updated and add an action, to redirect the user to a specific page.
You can however do the same in a light weight custom module using a form_alter hook.
First, find the form ID of the form. For node forms, it's the [node-type]_node_form.
Then, you can add a new submit function to be executed when the form is submitted. In this submit handler, set the redirect path.
See this guide on a basic how-to on creating a module.
Your module code would be something like belows:
<?php
function mymodule_mytype_node_form_alter(&$form, &$form_state) {
$form['#submit'][] = 'mymodule_node_submit_do_redirect';
}
function mymodule_node_submit_do_redirect($form, &$form_state) {
$form_state['redirect'] = 'my_custom_destination';
}
A much much simpler approach is to set the destination in the node form's URL.
For example, if you opened http://example.com/node/add/mytype?destination=my_custom_destination, you will be redirected to that URL instead of the node view page.
This works for me using Drupal 7, after creating a new module!
Put into the .module file the following PHP code:
<?php
function custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == '[node-type]_node_form') {
$form['actions']['submit']['#submit'][]= 'my_custom_submit_handler';
}
}
function my_custom_submit_handler($form, &$form_state) {
$form_state['redirect'] = 'http://www.expamle.eu/?q=node/2';
}
You just need to change [node-type]_node_form with your node type name (e.g.: example_node_form) and the http://www.expamle.eu/?q=node/2 with the correct URL.

How to programatically disable regions on a drupal 7 page?

I am working on a module where i have a page that must have no regions or extra content. A kind of "please wait" page.
How do i diable all extra content (regions menus...etc) ? i think Panels has this ability but i can't find the snippet it uses.
On another hand is it possible for a module to specify a special custom page ? like the maintenance-page for example ?
The page.tpl.php method is not flexible. It is based on a presentation logic. You should use hook_page_alter() for a business logic solution. For example:
function yourmodulename_page_alter(&$page) {
if (current_path() == 'node/add/yourcontenttype') {
unset($page['sidebar_first']);
}
}
Also look at very powefull Context module.
You can create a an extra page.tpl.php specifically for the page where you want to hide the regions. The naming principle is similar to the one for nodes.
Let's say you have a page with the url example.com/content/contact. A template named page--content--contact.tpl.php would serve that page and any page that starts with that url, i.e. the page example.com/content/contact/staff would also use that template (I think).
Check the classes of the body element for clues to what you can name your template, most themes will print that. In my example above, the body element would include the class page-content-contact.
Only thing i can think of is writing checks in your page.tpl.php file to see if you on that "page" your talking about and not printing out the regions/menus, or use a different template. http://drupal.org/node/223440
If you want to do this before the blocks are rendered:
/**
* Implements hook_block_list_alter()
*
* Hides the right sidebar on some pages.
*/
function THEME_NAME_block_list_alter(&$blocks) {
// This condition could be more interesting.
if (current_path() !== 'node/add/yourcontenttype') {
return;
}
// Go through all blocks, and hide those in the 'sidebar_second' region.
foreach ($blocks as $i => $block) {
if ('sidebar_second' === $block->region) {
// Hide this block.
unset($blocks[$i]);
}
}
}
Note: Interestingly, this hook seems to work no matter if you have it in your theme or in a module.
(Please correct me if I'm wrong)

custom page.tpl.php for a drupal view

How can I create a custom page.tpl.php for a specific view?
I'm not talking about styling the view itself, just the page where that view gets rendered.
Thank you.
#Keith Morgan - It's page.
Per Allartk's solution
In Drupal 7 template.php:
function <theme>_preprocess_page(&$variables) {
if (($views_page = views_get_page_view()) && $views_page->name === "galleries") {
$variables['theme_hook_suggestions'][] = 'page__views__galleries';
}
}
Then in your theme directory, create the page--views--galleries.tpl.php file.
Clear drupal cache and template should work.
I used 'galleries' as and example views name.
You can create a template file to theme almost any aspect of views output. In your case you want to create a custom template for your page display.
On the view designer, click the Theme link in Basic Settings. You'll see some template file naming options depending on if you want to theme the whole view (e.g., views-view--example--page.tpl.php), each row (e.g., views-view-fields--example--page.tpl.php) and so on.
Copy the appropriate template you want to customize from /sites/all/modules/views/theme to your theme and customize as you wish.
Once you create your custom template file, you can go back to the view designer Theme link to make sure it is being used. Your template should be bolded.
Hope this helps.
There's some documentation on template suggestions, and you might be interested in page.tpl.php suggestions. If it's a page view, you could use a path suggestion. So if your view is at http://www.example.com/photos, the page.tpl.php file would be named page-photos.tpl.php.
Had a similar problem, which was remedied by putting a .tpl.php in my theme's template folder. The naming convention for a page display of views in drupal 7 is page--path-to-view.tpl.php
In a drupal 7 theme I added a template suggestion in template.php:
function sovon_preprocess_page(&$variables) {
if(views_get_page_view()) {
$variables['theme_hook_suggestions'][] = 'page__view';
}
}
See http://api.drupalize.me/api/drupal/function/views_get_page_view/7 and http://drupal.org/node/223440#custom-suggestions for more information.
Assuming a view as a page, you can use the results of page_manager_get_current_page() in your preprocess to determine if your view is active, then take the appropriate steps (tack on body classes, add a template suggestion, etc).
using sillygwailo's suggestion I got this to work for me very nicely:
function YOUR-THEME-NAME_preprocess_page(&$vars) {
//allow template suggestions based on url paths.
$alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
if ($alias != $_GET['q']) { $suggestions = array();
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$suggestions[] = $template_filename;
}
$alias_array = explode('/', $alias);
$variables['template_files'] = $suggestions;
Then if your view is at www.example.com/photos , create a page-photos.tpl.php in your theme directory and drupal will use that as your template.
If that checked answer doesnt work(because it didnt for me) you can create a page--(urlname).tpl.php and then style your theme however you want it. Then you can import your view in one of several ways ...
print render($page['content']);
or
$view = views_get_view('viewname');
print $view->execute_display('default', $args);
$args is just an array and can be blank..you can just leave it off entirely

Resources