I am trying to make custom page template using these hooks in Drupal 7 but it shows blank page when i open in browser. Here is my code
/*
Implements hook_menu();
*/
function story_menu ()
{
$items['story/filters'] = array(
'title' => 'Search For stories',
'page callback' => 'story_filter_page',
'access arguments' => array('access content'),
);
return $items;
}
// Implements Page Callback
function story_filter_page ()
{
return theme('story_search_filter_page', array('title' => 'Testing'));
}
/*
Implements hook_theme();
*/
function story_theme($existing, $type, $theme, $path)
{
return array(
'story_search_filter_page' => array(
'variables' => array('title' => NULL),
'template' => 'custom-page',
),
);
}
I have created the template file : custom-page.tpl.php in my module directory.
I have figured it out why page is showing blank. basically story is my content type to so in my theme there is a tpl file name : page--story.tpl.php and that file was empty .. so that is why my pages showing me blank screen.
Related
i want to render my form in a twig template that overrides html.html.twig
the twig template is named html--resume--myform.html.twig
inside my form class i have this code
public function buildForm(array $form,FormStateInterface $form_state)
{
$form['#theme'] = ['html__resume__myform'];
$form['candidate_name'] = array(
'#type' => 'textfield',
'#title' => t('Candidate Name:'),
'#required' => TRUE,
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save2'),
'#button_type' => 'primary',
);
return $form
}
and inside my resume.module file i have this custom template
function resume_theme() {
return ['html__resume__myform' => ['render element' => 'form']];
}
but my form is not rendered in html--resume--myform.html.twig
or my $form variable is not passed to html--resume--myform.html.twig
html--resume--myform.html.twig is inside templates folder
I'm trying to create a custom module with configuration for a block that will allow a block to have custom fields. I'm having problems allowing the upload of an image and then rendering this in the block on the site.
Currently this is what my block file looks like;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a 'hello' block.
*
* #Block(
* id = "hello_block",
* admin_label = #Translation("Hello"),
* category = #Translation("Hello world block")
* )
*/
class HelloBlock extends BlockBase
{
/**
* {#inheritdoc}
*/
public function blockForm($form, FormStateInterface $formState)
{
$form['heading'] = array(
'#type' => 'textfield',
'#title' => t('Heading'),
'#description' => t('Enter the main heading'),
'#default_value' => 'Main heading'
);
$form['sub_heading'] = array(
'#type' => 'textfield',
'#title' => t('Sub heading'),
'#description' => t('Enter the sub heading'),
'#default_value' => 'Sub heading'
);
$form['body'] = array(
'#type' => 'text_format',
'#title' => t('Body'),
'#description' => t('Main body'),
'#format' => 'full_html',
'#rows' => 50,
'#default_value' => ''
);
$form['image'] = array(
'#type' => 'managed_file',
'#upload_location' => 'public://upload/hello',
'#title' => t('Image'),
'#upload_validators' => [
'file_validate_extensions' => ['jpg', 'jpeg', 'png', 'gif']
],
'#default_value' => isset($this->configuration['image']) ? $this->configuration['image'] : '',
'#description' => t('The image to display'),
'#required' => true
);
return $form;
}
/**
* {#inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $formState)
{
$this->configuration['heading'] = $formState->getValue('heading');
$this->configuration['sub_heading'] = $formState->getValue('sub_heading');
$this->configuration['body'] = $formState->getValue('body');
$this->configuration['image'] = $formState->getValue('image');
}
/**
* {#inheritdoc}
*/
public function build()
{
$markup = '<h1>'.$this->configuration['heading'].'</h1>';
$markup .= '<h2>'.$this->configuration['sub_heading'].'</h2>';
$markup .= '<img src="'.$this->configuration['image']['value'].'">';
$markup .= '<div>' . $this->configuration['body'] . '</div>';
return array(
'#type' => 'markup',
'#markup' => $markup,
);
}
}
Can anyone provide some pointers as to why the image isn't appearing? I'm assuming I'm missing something.
The text saved in the body (text_format) also appears in the block on the website as 'array', if anyone can help with that too it would be good, other wise I'll raise another question.
When you save that form, the value from the image field is the File ID.
Therefore, you can get the file object and the path by using:
$image = \Drupal\file\Entity\File::load($fid);
$path = file_create_url($image->getFileUri());
Then you would output the image using that path in your markup variable. There's probably a more semantic way to output the formatted image in a 'Drupal-ish' form, but this will get you started.
Oh, and the body field, use
$form_state->getValue('body')['value'];
in your markup.
BTW, I love using Devel and the ksm() function!
I have a custom Drupal 8 form that shows a list of users and a "check" button for each user.
When clicking a check button, the submit handler needs to figure out which users "check" button was clicked.
I've tried this the following way, but it always returns the id of the last element instead of the correct element.
Is this a bug in Drupal Core Form API?
Any other way to do this? I'm open to suggestions!
This is just an example. What I'm actually trying to do is show a list of users that belong to a specific 'company' node. In that list there is a 'remove from company' button for each user.
http://pastebin.com/us2YFcjr
<?php
namespace Drupal\form_multi_submit\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Entity\User;
class MultiSubmitForm extends FormBase {
public function getFormId() {
return 'MultiSubmitForm';
}
public function buildForm(array $form, FormStateInterface $form_state) {
// Get all users from database
$ids = \Drupal::entityQuery('user')
->condition('status', 1)
->execute();
$users = User::loadMultiple($ids);
// Set form table header
$form['users'] = array (
'#type' => 'table',
'#header' => array('ID', 'Remove'),
);
// Loop through all users
foreach ($users as $user) {
// Show user ID
$form['users'][$user->id()]['id'] = array(
'#type' => 'label',
'#title' => $user->id(),
);
// Show button for each user
$form['users'][$user->id()]['removememberbutton']['dummyNode'] = array(
'#type' => 'submit',
'#value' => 'Check',
'#submit' => array([$this, 'removeMember']),
);
}
return $form;
}
// Submit handler
public function removeMember(array &$form, FormStateInterface $form_state) {
$userid = $form_state->getTriggeringElement()['#array_parents'][1];
drupal_set_message($userid, 'status');
}
public function validateForm(array &$form, FormStateInterface $form_state) {
// Nothing to do here.
}
public function submitForm(array &$form, FormStateInterface $form_state) {
// Nothing to do here.
}
}
Drupal considers buttons with the same #value as the same button, unless they have a different #name
So all I had to do was add a unique #name to my buttons to get this to work properly:
$form['users'][$user->id()]['dummyNode-' . $user->id()] = array(
'#type' => 'submit',
'#value' => 'Check',
'#name' => 'check_' . $user->id(),
'#submit' => array([$this, 'removeMember']),
);
https://www.drupal.org/node/1342066#comment-11904090
I have problem with template for my custom page in my module.
I use hook_theme() to define my twig file. And when I check in hook_theme_registry_alter() I see my new template file but when I try use it it is not working.
My code :
file: first.module
/**
* Implement hook_theme().
*/
function first_theme($existing, $type, $theme, $path) {
return array(
'testtwig' => array(
'template' => 'testtwig',
'variables' => array('test_var' => NULL),
),
);
}
Controller:
/**
* #file
* Contains \Drupal\first\Controller\FirstController.
*/
namespace Drupal\first\Controller;
use Drupal\Core\Controller\ControllerBase;
class FirstController extends ControllerBase {
public function content() {
return array(
'#theme' => 'testtwig',
'#test_var' => t('sss'), //$output,
);
}
}
Error:
Template "modules/custom/first/templates/testtwig.html.twig" is not
defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to
find template "modules/custom/first/templates/testtwig.html.twig" in
the Drupal theme registry.).
//.module file
<?php
/**
* Implements hook_theme().
*/
function MODULE_theme($existing, $type, $theme, $path) {
return [
'site_framework_display' => [
'variables' => ['test_var' => NULL],
'template' => 'page--site-framework',
],
];
}
//Controller
<?php
namespace Drupal\MODULE\Controller;
use Drupal\Core\Controller\ControllerBase;
class MODULEController extends ControllerBase {
public function getVersion() {
return [
'#theme' => 'site_framework_display',
//'#test_var' => \DRUPAL::VERSION,
'#test_var' => 'hello guys'
];
}
}
I would like to return an array of string in my web services
I've tryed :
<?php
require_once('nusoap/nusoap.php');
$server = new soap_server();
$server->configureWSDL('NewsService', 'urn:NewsService');
$server->register('GetAllNews',
array(),
array('return' => 'xsd:string[]'),
'urn:NewsService',
'urn:NewsService#GetAllNews',
'rpc',
'literal',
''
);
// Define the method as a PHP function
function GetAllNews()
{
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
return $stack;
}
but it doesn't work.
What is the correct syntax for that ?
Thanks in advance for any help
You can't return an array like this. To return an array, you have to define a complex type.
I'll provide u an example...
The server program service.php:
<?php
// Pull in the NuSOAP code
require_once('lib/nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('RM', 'urn:RM');
//Define complex type
$server->wsdl->addComplexType(
'User',
'complexType',
'struct',
'all',
'',
array(
'Id' => array('name' => 'Id', 'type' => 'xsd:int'),
'Name' => array('name' => 'Name', 'type' => 'xsd:string'),
'Email' => array('name' => 'Email', 'type' => 'xsd:string'),
'Description' => array('name' => 'Description', 'type' => 'xsd:string')
)
);
// Register the method
$server->register('GetUser', // method name
array('UserName'=> 'xsd:string'), // input parameters
array('User' => 'tns:User'), // output parameters
'urn:RM', // namespace
'urn:RM#GetUser', // soapaction
'rpc', // style
'encoded', // use
'Get User Details' // documentation
);
function GetUser($UserName) {
return array('Id' => 1,
'Name' => $UserName,
'Email' =>'test#a.com',
'Description' =>'My desc'
);
}
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
And the client program client.php:
<?php
// Pull in the NuSOAP code
require_once('lib/nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/Service/service.php');
// Call the SOAP method
$result = $client->call('GetUser', array('UserName' => 'Jim'));
// Display the result
print_r($result);
?>