Accessing content of textarea in Drupal-7-Theme-Form - drupal-theming

in my custom theme-settings.php (zen-subtheme) i put following code to get a new textarea with textformat in my theme-settings:
<?php
function paper_form_system_theme_settings_alter(&$form, &$form_state) {
$form['paper_data'] = array(
'#type' => 'text_format',
'#title' => 'Put Text in here:',
'#rows' => 5,
'#resizable' => FALSE,
'#default_value' => 'xyz..',
'#format' => 'full_html'
);
}
the form is working perfektly, but when i want to access the variable by writing
<?php
$pdata = theme_get_setting('paper_data');
echo $pdata;
?>
in my page.tpl.php, the content of the variable is not rendered - instead the word "Array" is printed ...
What's wrong and why? (If i use 'textarea' as type instead of 'text_format', all is rendered well.)

You will understand when you use something like the Devel module's dpm() function to check the variable rather than echo(). Coding Drupal without the Devel module is, IMHO, folly.
The issue very likely stems from your use of the text_format type. As you can see, it saves both the textarea value as well as an associated text format. When this is used Drupal returns the data in structured form which varies depending on the type of format.
dpm() is your friend :)

Related

Drupal9: Saving form as node in SQL (custom module example)

i develop a custom module with forms to save data in SQL-Datebase. I want to use for that the node-structure.
Normal SQL-savings for example table works but not for the node-tables.
Any idea what is going wrong?
This ist my Code for saving, which works in non-node-tables:
public function submitForm(array &$form, FormStateInterface $form_state) { $connection = \Drupal::service('database');
$result = $connection->insert('node.node__body')
->fields(['body_value'])
->values([
'body_value' => 'text for body',
])
->execute();
$form_state->setRedirect('modulname.form');
}
Use Entity API in Drupal to manipulate or create a node.
In your case,
$node = \Drupal::entityTypeManager()->getStorage('node')->create(
[
'type' => 'page',
'title' => 'New Basic Page',
'body' => 'text for body'
]
);
Here, type is the content type machine name. Don't forget to update with your own. Also you probably want to inject the entity_type.manager service and use in the code.
Get more info here: Working with entities in Drupal

Add a new global variable in smarty? available on all pages. (prestashop)

I am looking to add a global variable in smarty.
I added css styles with {$urls.css_url} in stylesheep.tpl
<link rel="stylesheet" type="text/css" href="{$urls.css_url}my.css">
It works very well, except that if I put the shop on debug mode, I have this error:
ContextErrorException in smarty_internal_templatebase.php(157) : eval()'d code line 393: Notice: Undefined index: css_url
What does that mean? that this variable will not be available in smarty?, so if I put the cache, it will not be loaded?
How to make variable {$ urls.css_url} available with smarty? everywhere on the site?
Which code should I put and where to put it so that the variable (url_css) remains available in debug mode?
Thanks for your help
Thank you for your help, you have a high level compared to me.
In fact the variable is already defined and is available everywhere.
In frontcontroler.php, I had this:
$assign_array = array(
'img_ps_url' => _PS_IMG_,
'img_cat_url' => _THEME_CAT_DIR_,
'img_lang_url' => _THEME_LANG_DIR_,
'img_prod_url' => _THEME_PROD_DIR_,
'img_manu_url' => _THEME_MANU_DIR_,
'img_sup_url' => _THEME_SUP_DIR_,
'img_ship_url' => _THEME_SHIP_DIR_,
'img_store_url' => _THEME_STORE_DIR_,
'img_col_url' => _THEME_COL_DIR_,
'img_url' => _THEME_IMG_DIR_,
'css_url' => _THEME_CSS_DIR_,
'js_url' => _THEME_JS_DIR_,
'pic_url' => _THEME_PROD_PIC_DIR_,
);
I then added the next line below to assign it in smarty.
$this->context->smarty->assign(array('urls' => $urls));
The variable is displayed, no problem but if I put the shop in debug mode:
/* Debug only */
if (!defined('_PS_MODE_DEV_')) {
define('_PS_MODE_DEV_', true);
}
I always have this:
ContextErrorException in smarty_internal_templatebase.php(157) : eval()'d code line 393: Notice: Undefined index: css_url
Does this indicate that there is no value? Why does its value disappear? (when I activate PS_MODE_DEV)
Thanks again.
{$urls.css_url} means to retrieve the the css_url value from the urls array.
Also the Notice you receive means the variable has no value, so it can't work at all, so maybe what you think is correct, but what you see comes from other variable / hook.
How to set a Smarty Variable
In case you want to set a smarty variable you will have first to assign it.
You can do it through a module, controller or a TPL.
From a module or a controller it will be available as soon as you declare it. To print or to debug it.
So, for example if you add it in the hookDisplayHeader or in actionFrontControllerSetMedia hooks the variable will available where you want to use it.
Also, to assign the value in a module or controller you will have to use a code like this:
$urls = array (
'css_url' => 'The URL to your CSS'
...
);
$this->context->smarty->assign(array('urls' => $urls));
Then you will be able to access it using {$urls.css_url}
How to know the already defined variables in Smarty
Enable the debug mode of your shop and then just add {debug} in any of your TPLs, this will show a Pop Up (remember to allow popups from your URL) with all the variables assigned to Smarty.
Maybe this is what you are looking for:
Version: Prestashop 1.7.2.4
We are going to do this overriding a class, in this case the following file: classes/controller/FrontController.php, this file assigns global variables for Prestashop.
Create a file: override/classes/controller/FrontController.php
Copy all the contents from: classes/controller/FrontController.php to our recently created file.
Check the following line: 1444, there is a function called getTemplateVarUrls(), inside this function there is already a variable css_url that points to theme css default dir : /public_html/themes/theme_name/assets/css
If you want to create a custom url variable write the code there, i.e:
public function getTemplateVarUrls()
{
$http = Tools::getCurrentUrlProtocolPrefix();
$base_url = $this->context->shop->getBaseURL(true, true);
//custom css url var points to a folder called "my_custom_css" in the root of the project
$customCssFolderUrl = $base_url.'/my_customs_css';
$urls = array(
'base_url' => $base_url,
'current_url' => $this->context->shop->getBaseURL(true, false).$_SERVER['REQUEST_URI'],
'shop_domain_url' => $this->context->shop->getBaseURL(true, false),
'custom_css_url' => $customCssFolderUrl ,
);
After we finish all this steps we proceed to delete public_html/app/cache/prod/class_index.php file, this to prevent Prestashop from ignoring our new file, to read more about this: http://doc.prestashop.com/display/PS16/Overriding+default+behaviors
Use your new variable: {$urls.custom_css_url} on any .tpl file
Hope this helps.

Yii, custom filter is not working

My custom filter isn't working. Can anyone please correct me?
In my
public function actionAdmin($mid=null) {
// the appropriate codes here...
$date = ">= ".date("Y-m-d");
$this->render('admin', array(
'model' => $model,
'mid' => $mid,
'date'=>$date,
));
}
In my admin.php, I added this line in the appropriate field, in this case, dateEnd.
UPDATED
array(
'name'=>'dateEnd',
'htmlOptions'=>array('width'=>'150px'),
'filter'=>array('0'=>'', '1'=>$date),
),
Okay, so here's the problem. No matter what I click on, it's not filtering anything. I want it to filter either a blank space OR a date of today.
Can I please know what have I done wrong? Please feel free to correct me. Thanks!
You can't access passed variable inside cgridview. For this purpose, you can define a global variable inside your controller, and access it inside cgridview, something like this:
class Yourcontroller extends Controller {
public $date;
public function actionAdmin($mid=null) {
// the appropriate codes here...
$this->date = ">= ".date("Y-m-d");
$this->render('admin', array(
'model' => $model,
'mid' => $mid
));
}
}
Now, you can access date inside grid:
array(
'name'=>'dateEnd',
'htmlOptions'=>array('width'=>'150px'),
'filter'=>array('0'=>'', '1'=>$this->date),
),
Change this line :
'filter'=>array('0'=>'', '1'=>$date),
To this line:
'filter' => array(">= ".date("Y-m-d") => Yii::t('app', 'Still On Leave')),
Basically the first parameter is the real value, where as the second part is the View to be displayed.
In the First line, '1'=>$date means 1 value but to display a $date field on the CGridView's filter. It will take the value 1 to filter instead of the $date field.
Treat it like the Select element in HTML, where you have this line:
<select>
<option value="1">One</option>
<option value="2">Two</option>
</select>
Javascript basically takes the value instead of the word One being displayed.
This is the same explanation for it.

Creating Prestashop back-office module with settings page

I'm creating a back-office module for Prestashop and have figured out everything except the best way to display the admin page. Currently I'm using the renderView() method to display the content of view.tpl.
I would like to display a table with values and an option to add a new row. Should I just create it in the view.tpl or is there a better way? I've seen the renderForm() method but haven't figured out how it works yet.
The biggest question I have is, how do I submit content back to my controller into a specific method?
ModuleAdminController is meant for managing some kind of records, which are ObjectModels. Defauly page for this controller is a list, then you can edit each record individually or view it's full data (view).
If you want to have a settings page, the best way is to create a getContent() function for your module. Besides that HelperOptions is better than HelperForm for this module configuration page because it automatically laods values. Define the form in this function and above it add one if (Tools::isSubmit('submit'.$this->name)) - Submit button name, then save your values into configuration table. Configuration::set(...).
Of course it is possible to create some sort of settings page in AdminController, but its not meant for that. If you really want to: got to HookCore.php and find exec method. Then add error_log($hook_name) and you will all hooks that are executed when you open/save/close a page/form. Maybe you'll find your hook this way. Bettter way would be to inspect the parent class AdminControllerCore or even ControllerCore. They often have specific function ready to be overriden, where you should save your stuff. They are already a part of execution process, but empty.
Edit: You should take a look at other AdminController classes, they are wuite simple; You only need to define some properties in order for it to work:
public function __construct()
{
// Define associated model
$this->table = 'eqa_category';
$this->className = 'EQACategory';
// Add some record actions
$this->addRowAction('edit');
$this->addRowAction('delete');
// define list columns
$this->fields_list = array(
'id_eqa_category' => array(
'title' => $this->l('ID'),
'align' => 'center',
),
'title' => array(
'title' => $this->l('Title'),
),
);
// Define fields for edit form
$this->fields_form = array(
'input' => array(
array(
'name' => 'title',
'type' => 'text',
'label' => $this->l('Title'),
'desc' => $this->l('Category title.'),
'required' => true,
'lang' => true
),
'submit' => array(
'title' => $this->l('Save'),
)
);
// Call parent constructor
parent::__construct();
}
Other people like to move list and form definitions to actual functions which render them:
public function renderForm()
{
$this->fields_form = array(...);
return parent::renderForm();
}
You don't actually need to do anything else, the controller matches fields to your models, loads them, saves them etc.
Again, the best way to learn about these controller is to look at other AdminControllers.

How does one add a 'plain text node' to a zend form?

I'm trying to add a plain text node in a zend form - the purpose is to only dispay some static text.
The problem is - im not aware of any such way to do it.
I have used 'description' but that HAS to be attached to a form element.
Is there any way to simply display some text as part of a form? Zend considers everything as a form element so I cannot just print it out.
Eg:
The following will test your ability on so and so.
.
.
.
etc...
Any thoughts?
Zend has a form note view helper (Zend_View_Helper_FormNote), which you can use to add text.
Just create a new form element (/application/forms/Element/Note.php):
class Application_Form_Element_Note extends Zend_Form_Element_Xhtml
{
public $helper = 'formNote';
}
In your form:
$note = new Application_Form_Element_Note(
'test',
array('value' => 'This is a <b>test</b>')
);
$this->addElement($note);
Adding a hidden element with non-escaped description does the thing.
$form->addElement('hidden', 'plaintext', array(
'description' => 'Hello world! Check it out',
'ignore' => true,
'decorators' => array(
array('Description', array('escape'=>false, 'tag'=>'')),
),
));
Works perfectly. It is still attached to an element, which is, however, not rendered this way.
Code taken from: http://paveldubinin.com/2011/04/7-quick-tips-on-zend-form/
There might be a better way, but I created a paragraph by using a custom form element and view helper. Seems like alot of code for something so simple. Please let me know if you've found a more simplistic way to do it.
//From your form, add the MyParagraph element
$this->addElement(new Zend_Form_Element_MyParagraph('myParagraph'));
class Zend_Form_Element_MyParagraph extends Zend_Form_Element
{
public $helper = 'myParagraph';
public function init()
{
$view = $this->getView();
}
}
class Zend_View_Helper_MyParagraph extends Zend_View_Helper_FormElement {
public function init() {
}
public function myParagraph() {
$html = '<p>hello world</p>';
return $html;
}
}
A little late but thought I'd throw it in anyway for the benefit of the community.
Aine has hit the nail on the head. FormNote is what you need if you want to use text in Zend_Form. However, you can use it without needing to extend Zend_Form_Element_Xhtml. See example below:
$text = new Zend_Form_Element_Text('myformnote');
$text->setValue("Text goes here")
->helper = 'formNote';
Note that you can use both text and html with the formNote helper.
This functionality is built into Zend via Zend_Form_Element_Note.
$note = new Zend_Form_Element_Note('forgot_password');
$note->setValue('Forgot Password?');
I faced the same problem and decided is better not to use Zend_Form at all, but to use directly view helpers (like Ruby on Rails does) and validate on the model.
This one-liner works for me:
$objectForm->addElement(new Zend_Form_Element_Note('note', array('value' => 'Hello World')));