Drupal form template suggestion - drupal

I'm trying to create a drupal form suggestion using :
function pfe_theme_suggestions_form_alter(array &$suggestions, array $variables) {
$suggestions[] = 'form__'. $variables['element']['#id'];
}
but after creating the form--user-login-form.html.twig , drupal doesn't take this template in consideration but it always uses form.html.twig.
This is the html comment :
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'form' -->
<!-- FILE NAME SUGGESTIONS:
* form--user-login-form.html.twig
x form.html.twig
-->
<!-- BEGIN OUTPUT from 'core/themes/stable/templates/form/form.html.twig' -->
After adding the HOOK_theme() :
function pfe_theme_suggestions_form_alter(array &$suggestions, array $variables) {
$suggestions[] = 'form__'.$variables['element']['#id']; //form--user-login-form.html.twig
}
function pfe_theme($existing, $type, $theme, $path) {
return [
'form__user-login-form' => [
'#theme' => 'form--user-login-form',
'render element' => 'form',
],
];
}
I get this error :
Twig_Error_Loader: Template "themes/pfe/templates/form--user-login-form.html.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "themes/pfe/templates/form--user-login-form.html.twig" in the Drupal theme registry.). in Twig_Loader_Chain->getCacheKey() (line 43 of /home/marwen/workspace/pfe/themes/pfe/templates/block/block--userlogin.html.twig).

Replace "-" with "_" using "str_replace", example:
$suggestions[] = 'form__' . str_replace('-','_', $variables['element']['#id']);

Related

How to access advanced custom field values within a gutenberg block?

I have a custom post type with a few advanced custom fields. I'm trying to access these custom field values from within a Gutenberg block.
I've added the following to my register_post_type function
'show_in_rest' => true,
'supports' => array( 'title', 'editor', 'custom-fields' ),
I can successfully retrieve the custom posts from within my Gutenberg block using:
select('core').getEntityRecords('postType', 'customType')
but I'm not seeing the custom fields or their values.
Here's my block's JavaScript:
const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const { withSelect } = wp.data;
registerBlockType('cgb/block-press-block', {
title: __('Press Block'),
icon: 'awards',
category: 'common',
keywords: [
__('press-block'),
],
edit: withSelect((select) => {
return {
posts: select('core').getEntityRecords('postType', 'press')
};
})(({posts}) => {
return <p>Content</p>;
}),
});
Is there a way to access the custom post's advanced field values on the editor side or a way to pass that data to the block?
As you're using Advanced Custom Fields already, are you able to rather than registering your own block independently, use acf_register_block instead? That way you can access fields from ACF in PHP based templates.
Here are some useful links about this:
ACF 5.8 – Introducing ACF Blocks for Gutenberg
acf_register_block()
This code is taken from the ACF blog post above and posted here for completeness in case the above link changes.
Register the ACF block:
add_action('acf/init', 'my_acf_init');
function my_acf_init() {
// check function exists
if( function_exists('acf_register_block') ) {
// register a testimonial block
acf_register_block(array(
'name' => 'testimonial',
'title' => __('Testimonial'),
'description' => __('A custom testimonial block.'),
'render_callback' => 'my_acf_block_render_callback',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array( 'testimonial', 'quote' ),
));
}
}
A callback function to include your block template:
function my_acf_block_render_callback( $block ) {
// convert name ("acf/testimonial") into path friendly slug ("testimonial")
$slug = str_replace('acf/', '', $block['name']);
// include a template part from within the "template-parts/block" folder
if( file_exists( get_theme_file_path("/template-parts/block/content-{$slug}.php") ) ) {
include( get_theme_file_path("/template-parts/block/content-{$slug}.php") );
}
}
The HTML of your block:
<?php
/**
* Block Name: Testimonial
*
* This is the template that displays the testimonial block.
*/
// get image field (array)
$avatar = get_field('avatar');
// create id attribute for specific styling
$id = 'testimonial-' . $block['id'];
// create align class ("alignwide") from block setting ("wide")
$align_class = $block['align'] ? 'align' . $block['align'] : '';
?>
<blockquote id="<?php echo $id; ?>" class="testimonial <?php echo $align_class; ?>">
<p><?php the_field('testimonial'); ?></p>
<cite>
<img src="<?php echo $avatar['url']; ?>" alt="<?php echo $avatar['alt']; ?>" />
<span><?php the_field('author'); ?></span>
</cite>
</blockquote>
<style type="text/css">
#<?php echo $id; ?> {
background: <?php the_field('background_color'); ?>;
color: <?php the_field('text_color'); ?>;
}
</style>
This creates a basic testimonials block as a simple starting point. ACF handles the JavaScript handling within Gutenberg so all you have to do is worry about the PHP side of things.
That means you get to use get_field() and the_field() function like we're (ACF fans) are used to. Mixing ACF and Gutenberg without using this native way may cause headaches and possibly require a plugin to access fields via the WordPress REST API.
Note: ACF support for Gutenberg blocks requires ACF version 5.8 or higher.

how to implement hook_theme drupal 8?

Im new to drupal,I need to render a form so i have to implement hook theme, my confusion is Under which directory I should create hook theme file in drupal 8?
// my_module.module
function custom_module_theme($existing, $type, $theme, $path) {
return array(
'customize_form' => array(
'variables' => array(
'Custom_Form' => NULL
),
'render element' => 'form'
),
);
}
where I have to put above file in drupal 8??
Thanks in advance.
In your .module file
File location - module/custom/MODULENAME/MODULENAME.module
/**
* #file
* Twig template for render content
*/
function MODULENAME_theme($existing, $type, $theme, $path) {
return [
'theme_name_template' => [
'variables' => ['flag' => NULL],
],
];
}
To Use theme function use below code
return ['#theme' => 'theme_name_template', '#flag' => 1];
If i got it right you want the folder to place your module, right? You have to put your module in a folder under
/modules/custom/your_module_folder or /sites/all/modules/your_module_folder

Drupal7 | Custom Module | How to Display Template?

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).

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