I've tried the simplest examples of using hook_menu() posted here and on other Drupal forms and noting seems to work. My code, in: /sites/themes/mytheme/mymodule.module, is as follows:
<?php
function helloworld_menu() {
$items = array();
$items['hello'] = array(
'title' => 'Hello world!',
'type' => MENU_CALLBACK,
'page callback' => 'helloworld_page',
'access callback' => TRUE,
);
return $items;
}
function helloworld_page() {
return 'Hello world !';
}
When I navigate to www.mydomain.com/hello I get a 404 error. I've tried enabling and disabling the module along with clearing the cache numerous times with no luck still. Here is some additional information about my environment:
Running Drupal Commerce version 7.22
I've enabled clean URLS and pathauto module
The end goal I'm trying to achieve is adding products to the cart with a link. I already have that part working so that I can pass product ID's into a function and add them to cart. I would be replacing helloworld_page() with my function and then changing $items['hello'] to $items['cart/add/%/%'], having two wildcards (product ID and quantity).
For a hook declaration like hook_menu, the function name should be like <your_module_name_here>_menu
This is where you are going wrong.
Your module name is mymodule.module so your hook_menu should be called, mymodule_menu
<?php
/**
* Implements hook_menu().
*/
function mymodule_menu() {
$items = array();
$items['hello'] = array(
'title' => 'Hello world!',
'type' => MENU_CALLBACK,
'page callback' => 'helloworld_page',
'access callback' => TRUE,
);
return $items;
}
function helloworld_page() {
return 'Hello world !';
}
Please correct the function name and clear your cache and try again.
ALso i noticed you put the module in an unconventional location.
Please move it to /sites/all/modules/custom/mymodule folder ( both mymodule.info and mymodule.module files ).
Related
I use a GridField in SilverStripe to create HTML section items that are rendered on a page. This works so far but it always displays the sections in the order that I added them to the CMS or rather by the ID it gets when it's created.
So my question is: How can i change that order. I don't want to manually change the IDs but would rather do a simple drag and drop.
Edit: Could the use of Elemental be a solution to this problem?
Screenshot of the CMS view
The Page:
class HomePage extends Page
{
private static $has_many = [
'SectionObjects' => SectionObject::class
];
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Sections',
$grid = GridField::create('SectionObjects', 'Sections', $this->SectionObjects(), GridFieldConfig_RecordEditor::create())
);
$config = $grid->getConfig();
$dataColumns = $config->getComponentByType(GridFieldDataColumns::class);
$dataColumns->setDisplayFields([
'ID' => 'ID',
'LastEdited' => 'Changed'
]);
return $fields;
}
}
The Section object
class SectionObject extends DataObject{
private static $db = [
'Content' => 'Text',
'BgColor' => Color::class
];
private static $has_one = [
'HomePage' => HomePage::class
];
public function getCMSFields(){
return new FieldList(
TextareaField::create('Content','SectionContent'),
ColorField::create('BgColor', 'Hintergrundfarbe')
);
}
}
Definitely using the elemental module would allow you to have the kind of behaviour you want - but if you don't want to go to the effort of refactoring your code to comply with that module, you could use either the gridfieldextensions or sortablegridfield module to allow you to sort the gridfield (with drag and drop) to your heart's content.
I have a custom Module that creates a custom Block which has field elements.
This all works fine but I need to theme this block. I have checked the other posts on here and tried with no luck.
I have enabled twig debug and got theme suggestions. Still no luck.
Can anyone please point me in the right direction.
This is what I have so far:
my_module/my_module.module
// nothing related in here
my_module/src/Plugin/Block/myModuleBlock.php
<?php
namespace Drupal\my_module\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a 'ModuleBlock' block.
*
* #Block(
* id = "module_block",
* admin_label = #Translation("My Module"),
* )
*/
class ModuleBlock extends BlockBase {
public function blockForm($form, FormStateInterface $form_state) {
$form['test'] = array(
'#type' => 'select',
'#title' => $this->t('test'),
'#description' => $this->t('test list'),
'#options' => array(
'Test' => $this->t('Test'),
),
'#default_value' => isset($this->configuration['test']) ? $this->configuration['test'] : 'Test',
'#size' => 0,
'#weight' => '10',
'#required' => TRUE,
);
return $form;
}
/**
* {#inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['test'] = $form_state->getValue('test');
}
/**
* {#inheritdoc}
*/
public function build() {
$build = [];
$build['module_block_test']['#markup'] = '<p>' . $this->configuration['test'] . '</p>';
return $build;
}
}
my_module/templates/block--my-module.html.twig // as suggested by twig debug
<h1>This is a test</h1>
<div id="test-widget">{{ content }}</div>
I should also note that in my my_theme.theme I have this but I don;t think its relevant:
// Add content type suggestions.
function my_theme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
if ($node = \Drupal::request()->attributes->get('node')) {
array_splice($suggestions, 1, 0, 'page__node__' . $node->getType());
}
}
As for what I've tried is this:
public function build() {
return array(
'#theme' => 'block--my-module'
);
}
But still no go.
Any help here is very much appreciated.
UPDATE: So I just got it to work but I still need help. I moved the template block--my-module.html.twig to my theme directory and it worked.
How do I get it to work in my module directory?
UPDATE: So I just got it to work but I still need help. I moved the
template block--my-module.html.twig to my theme directory and it
worked.
How do I get it to work in my module directory?
You can create a directory called templates/ in your modules root.
Place your template here.
Now let Drupal know you store the template in your module.
in your_module.module add this function:
function YOUR_MODULE_theme($existing, $type, $theme, $path) {
return array(
'block__my_module' => array(
'render element' => 'elements',
'template' => 'block--my-module',
'base hook' => 'block'
)
);
}
This is not tested. It´s the way it worked for my custom block.
Don´t forget to clear the cache.
To be able to add the twig file in your module, you need to make sure the module defines the reference, not the theme.
You can still implement hook_theme() in the module's .module file as follows:
function mymodule_theme($existing, $type, $theme, $path) {
return [
'mymodule_block' => [
'variables' => [
// define defaults for any variables you want in the twig file
'attributes' => [
'class' => ['my-module-class'],
], //etc
],
],
];
}
Then in your block's build() implementation you can add a reference to the new theme function:
public function build() {
// Load the configuration from the form
$config = $this->getConfiguration();
$test_value = isset($config['test']) ? $config['test'] : '';
$build = [];
$build['#theme'] = 'mymodule_block';
// You would not do both of these things...
$build['#test_value'] = $test_value;
$build['module_block_test']['#markup'] = '<p>' . $test_value . '</p>';
return $build;
}
Finally be careful about where you place your twig file and what you name it. Create a templates directory in your module directory, and replace the _ in the theme function name with -: mymodule-block.html.twig
I want to change the view of the module depending on the Url. All in php.
I created two views putting this data:
function config_services_block_info() {
$blocks['config_services'] = array(
// The name that will appear in the block list.
'info' => t('Services'),
// Default setting.
'cache' => DRUPAL_CACHE_PER_ROLE, );
$blocks['orderservices'] = array(
// The name that will appear in the block list.
'info' => t('Order Services'),
// Default setting.
'cache' => DRUPAL_CACHE_PER_ROLE, );
return $blocks;
}
function config_services_block_view($delta = '') { switch ($delta) {
case 'config_services':
...
block[content] = ...;
return block;
break;
case 'orderservices':
...
block[content] = ...;
return block;
break; } }
function config_services_menu() { $items = array();
$items['config_services/orderservices'] = array(
'title' => t('Order Services'),
'page callback' => array('_config_services_orderservices_page'),
'access arguments' => array('order config_services content'),
'type' => MENU_CALLBACK, //Will appear in block. ); return $items; }
In _config_services_orderservices_page() I think this but no work:
function _config_services_orderservices_page() {
config_services_block_view('orderservices');
}
The first view works, the problem is when I want the second view. How I change the view for the url: http:(slash)(slash)name-web/config_services/orderservices
Depending on the condition you want to change the view, the condition could be checked inside _config_services_orderservices_page() function, and specific block could be displayed.
function _config_services_orderservices_page() {
if (your_condition == 'orderservices') {
config_services_block_view('orderservices');
}
if (other_condition == 'config_services') {
config_services_block_view('config_services');
}
}
On a side note, view in Drupal means something totally different. It is the most popular and most used module overall. Please visit the project page, and the documentation.
And what you refer to is about blocks.
(Using Adaptive Theme) I want a new and custom user login page. So, put this code in my template.php:
function mythemename_theme() {
$items = array();
// create custom user-login.tpl.php
$items['user_login'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'mythemename') . '/templates',
'template' => 'user-login',
'preprocess functions' => array(
'mythemename_preprocess_user_login'
),
);
return $items;
}
Changed mythemename to my custom theme name. And created user-login.tpl.php files and put this code:
<?php
print drupal_render($form['name']);
print drupal_render($form['pass']);
print drupal_render($form['form_build_id']);
print drupal_render($form['form_id']);
print drupal_render($form['actions']);
?>
Lastly, cleared my site cache. But when i click mysite.com/user login page not changed.
Where the error am I doing? How can I solve this problem?
Try putting this in template.php, change mythemename to theme name, and flush the cache.
function mythemename_theme($existing, $type, $theme, $path){
$hooks['user_login']=array(
'render element'=>'form',
'template'=>'templates/user-login',
);
return $hooks;
}
Then in templates/user-login.tpl.php insert:
<?php
print render($form['name']);
print render($form['pass']);
print render($form['form_build_id']);
print render($form['form_id']);
print render($form['actions']);
?>
or
<?php print drupal_render_children($form) ?>
This is Drupal 6.x and am having a nightmare of a time to modify a simple drupal form. This is a module file.
function modulename_menu() {
$items = array();
$items['school/registration'] = array(
'title' => 'Registration Form',
'page callback' =>'drupal_get_form',
'type' => MENU_CALLBACK
);
return $items;
}//end of the function
function modulename_school_form_alter(&$form, $form_state, $form_id)
{
// dsm($form_id);
if ($form_id == 'user_registration_form')
{
// modify the "#submit" form property by prepending another submit handler arra
$form['#submit'] = array_merge(
array('_modulename_registration_submit' => array()),
$form['#submit']
);
}
}
For what it's worth a few months afterwards - I just had the same problem. Check out
http://drupal.org/node/626834#comment-2393090
You probably have an install file with your custom module that doesn't contain all the necessary information.