Custom Drupal 7 Module not appearing in Configuration - php

I've been using this guide to develop a Drupal 7 module.
I want this module to simply display them to an administrator who can then change and accept them from there. I can get my module to appear in the Modules section but when I enable it, the form and menu item I built are not where they should be. There is no menu item in the Configuration section so I can't navigate to the form I made. Here's my .module:
/**
* Implements hook_help.
*
* Displays help and module information.
*
* #param path
* Which path of the site we're using to display help
* #param arg
* Array that holds the current path as returned from arg() function
*/
function moderate_submissions_help($path, $arg) {
switch ($path) {
case "admin/help#moderate_submissions":
return '<p>' . "Allows admins to moderate new pending submissions." . '</p>';
break;
}
}
/**
* Implements hook_menu().
*/
function moderate_submissions_menu() {
$items = array();
$items['admin/config/content/moderate_submissions'] = array(
'title' => 'Moderate Submissions',
'description' => 'Go through submissions.',
'page callback' => 'drupal_get_form',
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
}
/**
* Page callback: Settings
*
* #see moderate_submissions_menu()
*/
function moderate_submissions_form($form, &$form_state) {
$form['moderate_submissions_max'] = array(
'#type' => 'textfield',
'#title' => t('Maximum number of posts'),
'#size' => 2,
'#maxlength' => 2,
'#description' => t('The maximum number of links to display in the block.'),
'#required' => TRUE,
);
return system_settings_form($form);
}
And my .info:
name = Moderate Submissions
description = Moderate pending goal submissions.
core = 7.x
configure = admin/config/content/moderate_submissions
This is probably the result of something I overlooked in trying to adapt the tutorial to what I'm building.

moderate_submisisons_menu() needs to return $items.

Related

Why does my custom code show warnings in the logs?

I have a site on Drupal 8.6 and Bootstrap 3.3.7
I created a custom module for the customer to accept the terms and conditions of the store when placing an order.
This displays a link to a checkbox before payment to display the terms and conditions in a modal window.
When I place an order here are the warnings in the logs (sorry my code is too long to be published here):
https://pastebin.com/1p5m1Ved
https://pastebin.com/XYbqDJje
https://pastebin.com/P93bStKh
Here is the file that created the problem :
<?php
namespace Drupal\commerce_marketplace_terms_and_conditions\Plugin\Commerce\CheckoutPane;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
/**
* Provides the completion message pane.
*
* #CommerceCheckoutPane(
* id = "marketplace_terms_and_conditions",
* label = #Translation("Marketplace Terms and Conditions"),
* default_step = "review",
* )
*/
class MarketplaceTermsAndConditions extends CheckoutPaneBase implements CheckoutPaneInterface {
/**
* {#inheritdoc}
*/
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
$store_name = $this->order->getStore()->getName();
$store_id = $this->order->getStoreId();
$pane_form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$attributes = [
'attributes' => [
'class' => 'use-ajax',
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => auto
]),
],
];
$link = Link::fromTextAndUrl(
$this->t('terms and conditions of the store "#store_name"', ['#store_name' => $store_name]),
Url::fromUri("internal:/store/$store_id/cgv", $attributes)
)->toString();
$pane_form['marketplace_terms_and_conditions'] = [
'#type' => 'checkbox',
'#default_value' => FALSE,
'#title' => $this->t('I have read and accept #terms.', ['#terms' => $link]),
'#required' => TRUE,
'#weight' => $this->getWeight(),
];
return $pane_form;
}
}
What's wrong with my custom module and how to fix the problem ? Thank you
Change this to
'width' => auto
this
'width' => 'auto'
It is assuming it to be a constant, as you can see from the errors, it has to be either a variable or a string.

Assist with Drupal Admin Blank page for custom functions or code

I am trying to learn Drupal. What I want to do is create a backend page (that is on main menu) where I can run my own functions and code. I have been doing research and found out that to do this I need to create a module. And if I run the "hook_menu" function - i can get that backend page to be on the menu. I found code for a drupal module that does this, and it loads a form for a "config settings" page. Here is the code:
function add_game_menu() {
$items = array();
$items['admin/add_game'] = array(
'title' => 'Add Gm Pg',
'description' => 'Description of your add game page',
'page callback' => 'drupal_get_form',
'page arguments' => array('add_game_admin'),
'access arguments' => array('administer add_game settings'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function add_game_admin() {
$form = array();
$form['add_game_maxdisp'] = array(
'#type' => 'textfield',
'#title' => t('Maximum number of links'),
'#default_value' => variable_get('add_game_maxdisp', 3),
'#size' => 2,
'#maxlength' => 2,
'#description' => t("The maximum number of links to display in the block."),
'#required' => TRUE,
);
return system_settings_form($form);
}
I modified it a little but it does work. What I want to do is do this but run my own functions and code on here and not the "drupal_get_form" function.
I tried to do this and just created a function to echo text and then put the function name in the "page callback" field of the array. This did work, it did execute my function on the page instead of the drupal form function, but the page was a blank white page with none of the "drupal backend styling or menus or anything"; it was literally just completely blank white webpage with just my text printed on it.
So I am thinking the "drupal_get_form" function not only puts a form on the page, but it also makes it so it is drupal backend page with proper header, footer, menus etc.
So I am thinking that i need a function like "drupal_get_form" but it has a "blank slate" where I can run whatever code or functions that I want.
Would anybody know anything about this or how to approach doing this?
Thanks so much...
Everything you need is to create a template, register it and use theme function in your code. You can take a look at this Quick Introduction to Drupal's hook_menu() and hook_theme()
So your code might look like:
function add_game_menu() {
$items = array();
$items['admin/add_game'] = array(
'title' => 'Add Gm Pg',
'description' => 'Description of your add game page',
'page callback' => 'my_function',
'access arguments' => array('administer add_game settings'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function my_function(){
// Call theme() function, so that Drupal includes the custom-page.tpl.php template
return theme('my_custom_template');
}
/*
* Implementation of hook_theme().
*/
function add_game_theme(){
return array(
'my_custom_template' => array(
// template file name will be custom-page.tpl.php
'template' => 'custom-page',
),
);
}

Drupal CTools access check

I want to create a ctools access check for my panel selection rule.
What I wanna do, is to check a field value in a content type. The field is named field_layout with the options 3,2,1.
I created the access check and settings and the rule is showing up in the selection rule options. I can add it without any problems and set it up as I want to.
The only problem I have is, that the rule wont take effect ... :-/
Here is the code I use:
<?php
/**
* Plugins are described by creating a $plugin array which will
* be used by the system that includes the file.
*/
$plugin = array(
'title' => t('Node: field layout'),
'description' => t('Controls access by field_layout'),
'callback' => 'he_layout_field_layout_ctools_access_check',
'settings form' => 'he_layout_field_layout_ctools_settings',
);
/**
* Custom callback defined by 'callback' in the $plugin array.
*
* Check for access.
*/
function he_layout_field_layout_ctools_access_check($conf, $context) {
// If for some unknown reason that $context isn't set, we just want to be sure.
if (empty($context) || empty($context->data) || empty($context->data->field_layout)) {
return FALSE;
}
// If the layout set in the panels visibility rule settings is different from the field_layout
// access to the pane is denied.
$layout = $context->data->field_layout;
if ($layout !== $conf['field_layout'][$context->data->field_layout[field_language('node', $context->data, 'field_layout')][0]['value']]) {
return FALSE;
}
return TRUE;
}
/**
* Settings form for the 'field_layout' access plugin.
*/
function he_layout_field_layout_ctools_settings($form, &$form_state, $conf) {
$form['settings']['field_layout'] = array(
'#type' => 'radios',
'#title' => t('Layout'),
'#options' => array(
0 => '3',
1 => '2',
2 => '1',
),
'#default_value' => $conf['field_layout'],
);
return $form;
}
The code is based on this tutorial:
http://ramlev.dk/blog/2012/03/30/create-a-ctools-access-plugin/
Someone got an idea why this wont work?
#Basti's comment is correct, just one more step up:
$plugin = array(
'title' => t('Node: field layout'),
'description' => t('Controls access by field_layout'),
'callback' => 'he_layout_field_layout_ctools_access_check',
'settings form' => 'he_layout_field_layout_ctools_settings',
// 'required context' => new ctools_context_required(t('Node'), 'node'),
);
It is ok if don't need the context for your plugin. But the $context argument in the access check receives exactly the context you mentioned, which means you always get null when you specify no required context.
This way, you alway have false at the first check from this: if (empty($context)

drupal ajax callback on language selection

Is there way to bind an ajax callback when the language is changed, i want to update a nodereference-dropdown when a language is changed (to show the values only in that language).
following code is not working (form_alter) although other callbacks are working.
Can someone help me how can i achieve that?
$form['language']['#ajax'] = array(
'callback' => 'mymodule_something_language_callback',
'wrapper' => 'my-module-replace',
'#weight' => 2
);
Thanks.
FROM COMMENTS
heres the var_dump of $form['language'];
array
'#type' => string 'select' (length=6)
'#title' => string 'Language' (length=8)
'#default_value' => string 'und' (length=3)
'#options' =>
array
'und' => string 'Language neutral' (length=16)
'en' => string 'English' (length=7)
'ar' => string 'Arabic' (length=6)
The problem is that the Locale module alters this form element after hook_form_alter() is called (see this post).
Here's how I solved this problem:
First, change the order in which Drupal implements its hooks, putting 'form_alter' as the last one:
<?php
/**
* Implementation of hook_module_implements_alter()
*/
function chronos_module_implements_alter(&$implementations, $hook) {
if ($hook == 'form_alter') {
// Move mymodule_form_alter() to the end of the list. module_implements()
// iterates through $implementations with a foreach loop which PHP iterates
// in the order that the items were added, so to move an item to the end of
// the array, we remove it and then add it.
$group = $implementations['chronos'];
unset($implementations['chronos']);
$implementations['chronos'] = $group;
}
}
Next, add a form element and the '#ajax' element that you want on $form['language']:
<?php
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'page_node_form') {
// alter the form
$form['container'] = array(
'#prefix' => '<div id="ajax-language">',
'#suffix' => '</div>',
);
$form['language']['#ajax'] = array(
'callback' => 'mymodule_save_language_callback',
'wrapper' => 'ajax-language'
);
return $form;
}
}
Lastly, add your callback:
<?php
/**
* Returns changed part of the form.
*
* #return renderable array
*
* #see ajax_example_form_node_form_alter()
*/
function chronos_save_language_callback($form, $form_state) {
# set session variables or perform other actions here, if applicable
return $form['container'];
}

Drupal Access denied in hook_menu implementation

I trying to create a drupal module, but when I go on page /polcode I get this notice:
Access denied
You are not authorized to access this page.
This is my module:
<?php
// $Id$
/**
* #file
* A module exemplifying Drupal coding practices and APIs.
*
* This module provides a block that lists all of the
* installed modules. It illustrates coding standards,
* practices, and API use for Drupal 7.
*/
/**
* Implements hook_menu().
*/
function polcode_menu()
{
$items['polcode'] = array
(
'title' => 'tytuł',
'description' => 'opis',
'page callback' => 'drupal_get_form',
'page arguments' => array('input_simple_form'),
'access calback' => TRUE,
);
return $items;
}
/*
* Form
*/
function input_simple_form($form, &$form_submit)
{
$form['color'] = array
(
'#title' => t('Color'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('Opis'),
);
$form['submit'] = array
(
'#type' => 'submit',
'#value' => 'submit',
);
return $form;
}
I had cleared cache, and enable module in backend, also I'm loged in as admin, what's wrong?
And this is info:
;$Id$
name = polcode
description = A first module.
package = Drupal 7 Development
core = 7.x
files[] = polcode.module
;dependencies[] = autoload
;php = 5.2
You misspelled access callback, change it from:
'access calback' => TRUE,
to be:
'access callback' => TRUE,

Categories