I want to be able to pass a node id to a path and generate an RTF file. I am having problems loading the library. I am using the Libraries 2 method. The error is:
PHP Fatal error: Class 'PHPRtfLite' not found in [redacted]/sites/all/modules/custom/ain_export/ain_export.module on line 22 request_id="245a0131-9519-4fa9-a512-3b60d0b91ad8"
So I've been making sure the files are where they should be. In my /sites/all/libraries directory I have a folder called 'phprtflite', set up like this:
-- phprtflite
----PHPRtfLite.php
Below is my code, now edited per the comment below to load the library inside my callback. I am still getting the error, except now on line 41. It seems to not be loading the library?
<?php
/**
* Implements hook_libraries_info().
*/
function ain_export_libraries_info() {
$libraries['phprtflite'] = array(
'name' => 'PHPRtfLite',
'vendor url' => 'http://sourceforge.net/projects/phprtf/',
'download url' => 'http://sourceforge.net/projects/phprtf/',
'files' => array(
'php' => array('PHPRtfLite.php'),
),
);
return $libraries;
}
/**
* Implements hook_menu().
*/
function ain_export_menu() {
$items = array();
$items['admin/ain_export/%'] = array(
'title' => 'Export Node',
'page callback' => 'ain_export_page',
'page arguments' => array(2),
'access arguments' => array('access administration pages'),
);
return $items;
}
// spit out node
function ain_export_page($nid) {
// load library
libraries_load('phprtflite');
// register PHPRtfLite class loader
PHPRtfLite::registerAutoloader();
if (isset($nid) ) {
try {
$rtf = new PHPRtfLite();
// set page properties
$rtf->setMargins(2.54, 2.54, 2.54, 2.54);
$rtf->setPaperFormat(PHPRtfLite::PAPER_LETTER);
// define fonts
$fontH1 = new PHPRtfLite_Font(16, 'Arial', '#000000');
$fontH2 = new PHPRtfLite_Font(14, 'Arial', '#000000');
$fontP = new PHPRtfLite_Font(12, 'Arial', '#000000');
// vertical space
$formatH1 = new PHPRtfLite_ParFormat();
$formatH1->setSpaceAfter(8);
$formatH2 = new PHPRtfLite_ParFormat();
$formatH2->setSpaceAfter(6);
$formatP = new PHPRtfLite_ParFormat();
$formatP->setSpaceAfter(3);
// page content
$node = node_load($nid); // load the node
// headline
$section = $rtf->AddSection();
$section->writeText($node->title, $fontH1, $formatH1);
// output file
$rtf->SendRtf( $nid . '.rtf' );
} catch (Exception $e) {
$error = $e->getMessage();
}
}
}
Related
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.
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 ).
I cant find why my code isn't working properly. I try to upload the image through the form and save it in a directory, but it isnt there. For a test I did echo on the file object from the form to display the name of the file and extension and its correct, but its not saving.
Here is my code:
Form:
class WgrajForm extends sfForm
{
public function configure()
{
$this->setWidgets(array(
'zdjęcie' => new sfWidgetFormInputFile(),
'nazwa' => new sfWidgetFormInput(),
));
$this->setValidators(array(
'zdjęcie' => new sfValidatorFile(array(
'mime_types' => 'web_images',
'path' => '/projektSymfony/web/images',
),array(
'mime_types' => 'Plik może być tylko zdjęciem'
)
),
'nazwa' => new sfValidatorString(array(), array('required' => 'pole wymagane')),
));
$this->getWidgetSchema()->setNameFormat('wgraj[%s]');
}
}
Action:
public function executeDodaj(sfWebRequest $request)
{
$this->form= new WgrajForm();
if ($request->isMethod('post'))
{
$this->form->bind(
$request->getParameter('wgraj'),
$request->getFiles('wgraj'));
if ($this->form->isValid())
{
$wgrane = $this->form->getValues();
$plik = $wgrane['zdjęcie'];
$pliknazwa = $wgrane['nazwa'];
$rozszerzenie = $plik->getExtension($plik->getOriginalExtension());
$plik->save('/projektSymfony/web/images/'.$pliknazwa.$rozszerzenie);
//$this->redirect('galeria/index');
}
}
}
The path parameter is better when you use sfConfig::get('sf_upload_dir') or sfConfig::get('sf_web_dir') in your case.
You don't need to save() the widget inside your action. You just need to save the form, and then, it will save your image.
if ($this->form->isValid())
{
$this->form->save();
$this->redirect('galeria/index');
}
Take a look at the documentation, you will find lots of information, specially the part of Jobeet tutorial about 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.