I've created a module which defines a new text format filter.
Now I want to define a text format using this new filter, directly from module php. Drupal Administrator can do this manually from admin/config/content/formats/add admin page, but I want to avoid this step. What do I need to add to my_dmodule.module?
I'm guessing that you are trying to create a text format on module installation. If so, you could call filter_format_save(). The details on creating the object can be found in filter.module (Drupal API reference).
You might have to load the filter module first if you are creating the filter in hook_install(), haven't checked:
drupal_load('module', 'filter');
Inspired by Hendrik's answer, this is my solution:
function myformat_install() {
drupal_load('module', 'filter');
/* check already exists */
$format_exists = (bool) db_query_range('SELECT 1 FROM {filter_format} WHERE name = :name', 0, 1, array(':name' => 'My Format'))->fetchField();
if (!$format_exists) {
$format = array(
'format' => 'myformat',
'name' => 'My Format',
'filters' => array(
'myformat_filter' => array(
'weight' => 0,
'status' => 1,
),
),
);
$format = (object) $format;
filter_format_save($format);
}
}
myformat_filter is a filter defined implementing hook_filter_info(), but it could be a filter defined in another module.
Related
I'm trying to add a new field to Admin Preferences - a textarea field with tinymce. I've added code to AdminPreferencesController.php:
$this->fields_options['contact'] = array(
'title' => $this->l('Contact'),
'icon' => 'icon-cogs',
'submit' => array('title' => $this->l('Save')),
);
$this->fields_options['contact']['fields']['PS_CONTACT_ADDITIONAL_INFO'] = array(
'type' => 'textarea',
'label' => $this->l('Short description'),
'name' => 'short_description',
'lang' => true,
'cols' => 60,
'rows' => 10,
'autoload_rte' => 'rte',
'col' => 6,
);
But tinymce doesnt' appear and when I'm using HTML tags after saving they disappear. Presta strips all HTML tags.
How to allow HTML tags on this field and enable tinymce?
It seems that you can't just add it in a regular way. But you can implement it in a next way.
First of all, use field type textareaLang instead of textarea and add a parameter 'validation' => 'isCleanHtml' to this field
$this->fields_options['contact']['fields']['PS_CONTACT_ADDITIONAL_INFO'] = array(
'type' => 'textareaLang',
'label' => $this->l('Short description'),
'name' => 'short_description',
'lang' => true,
'cols' => 60,
'rows' => 10,
'col' => 6,
'validation' => 'isCleanHtml'
);
Create your own script to initialize your editor. I created a script tinymce.init.js and put it to js/admin/ folder
$(document).ready(function(){
ad = ''; // this is defenition of the external plugin path. I didn't fint how it can impact on script if it's empty but by default it it the path to your admin folder
iso = iso_user;
var config = {
selector: '.textarea-autosize'
};
tinySetup(config);
});
Then include tinymce script and your own to this controller AdminPreferencesController.php
public function setMedia()
{
$this->context->controller->addJquery();
$this->context->controller->addJS(
array(
_PS_JS_DIR_.'admin/tinymce.init.js',
_PS_JS_DIR_.'tiny_mce/tiny_mce.js',
_PS_JS_DIR_.'admin/tinymce.inc.js'
)
);
parent::setMedia();
}
It should implement your requirements. But don't forget that now you should call your configuration field in multilingual scope. So, add a language id to Configuration::get() like
Configuration::get('PS_CONTACT_ADDITIONAL_INFO, $id_lang)
whenever you use it.
P.S. Bear in mind that the best solution for your goal is to create a simple module which will handle this. And far more, it is recommended way.
I have the following situation: Contacts without a first or last name, in fact, they only have a email address.
I can work with these contacts fine, but when I use the listview anywhere (for instance to show all contacts from a company) there now is no way to click through to the contact (normally you would click on the name).
I'm looking for a way to solve this, for instance by showing a clickable text like 'name not known', but can't figure out how to do this. I've been looking at the manual and in the files in the modules directory and the sugarfields dir, but can't quite figure it out.
The closest I got was in /sugarcrm/modules/Contacts/metadata/listviewdefs.php
where this piece of code resides:
$listViewDefs['Contacts'] = array(
'NAME' => array(
'width' => '20%',
'label' => 'LBL_LIST_NAME',
'link' => true,
'contextMenu' => array('objectType' => 'sugarPerson',
'metaData' => array('contact_id' => '{$ID}',
'module' => 'Contacts',
'return_action' => 'ListView',
'contact_name' => '{$FULL_NAME}',
'parent_id' => '{$ACCOUNT_ID}',
'parent_name' => '{$ACCOUNT_NAME}',
'return_module' => 'Contacts',
'return_action' => 'ListView',
'parent_type' => 'Account',
'notes_parent_type' => 'Account')
),
'orderBy' => 'name',
'default' => true,
'related_fields' => array('first_name', 'last_name', 'salutation', 'account_name', 'account_id'),
),
Somewhere there has to be a function that joins the first and lastname together...
Edit: I found a solution:
The actual concatenation function is in /sugarcrm/include/SugarObjects/templates/person/person.php and is called _create_proper_name_field()
I can modify the output for my specific case by adding something like this to the end of the function:
if (empty(trim($full_name))){
$full_name = 'Name unknown';
}
However, I would rather have a upgrade safe solution, so that will be the next challenge.
Don't edit the core because the next upgrade will break your SugarCRM instance. Use logic hooks to be upgrade safe:
create a file 'logic_hooks.php' in /custom/modules/Contacts/
In that file, add the followin code:
<?php
$hook_array['before_save'][] = Array(1,'logic_fill_name','custom/modules/Contacts/logic_hooks/logics.php','ContactLogics','logic_fill_name');
After you have done this. create the file 'logics.php' in /custom/modules/Contacts/logic_hooks.
In the logics.php file, add something like:
<?php
require_once 'include/SugarQuery/SugarQuery.php';
/**
* Class ContactLogics
*/
class ContactLogics {
/**
* #param $bean
* #param $event
* #param $arguments
*/
public function logic_fill_name($bean, $event, $arguments) {
if (empty(trim($bean->first_name)) && empty(trim($bean->last_name))){
$bean->last_name = 'Name unknown';
}
}
}
Now some explanation. When you edited a recordview and pressed the save button, the logic hook 'before_save' will be triggered. This code will change the full name to 'Name unknown' when the full name is empty. When the 'before_save' is executed, the actual save will take place.
I'm designing a new module in Drupal 8. It's a long-term project that won't be going public for a few months at least, so I'm using it as a way to figure out what's new.
In this module, I want to be able to programmatically create nodes. In Drupal 7, I would do this by creating the object, then calling "node_submit" and "node_save".
These functions no longer exist in Drupal 8. Instead, according to the documentation, "Modules and scripts may programmatically submit nodes using the usual form API pattern." I'm at a loss. What does this mean? I've used Form API to create forms in Drupal 7, but I don't get what the docs are saying here.
What I'm looking to do is programmatically create at least one and possibly multiple new nodes, based on information not taken directly from a user-presented form. I need to be able to:
1) Specify the content type
2) Specify the URL path
3) Set any other necessary variables that would previously have been handled by the now-obsolete node_object_prepare()
4) Commit the new node object
I would prefer to be able to do this in an independent, highly abstracted function not tied to a specific block or form.
So what am I missing?
use Drupal\node\Entity\Node;
$node = Node::create(array(
'type' => 'your_content_type',
'title' => 'your title',
'langcode' => 'en',
'uid' => '1',
'status' => 1,
'field_fields' => array(),
));
$node->save();
RE: deprecated entity create
Here is a short example of usage without the deprecated functions. This is particularly helpful for dynamic creation:
//define entity type and bundle
$entity_type="node";
$bundle="article";
//get definition of target entity type
$entity_def = \Drupal::entityManager()->getDefinition($entity_type);
//load up an array for creation
$new_node=array(
//set title
'title' => 'test node',
//set body
'body' => 'this is a test body, can also be set as an array with "value" and "format" as keys I believe',
//use the entity definition to set the appropriate property for the bundle
$entity_def->get('entity_keys')['bundle']=>$bundle
);
$new_post = \Drupal::entityManager()->getStorage($entity_type)->create($new_node);
$new_post->save();
The Drupal 8 version of devel/devel_generate module has a good example of this.
$edit_node = array(
'nid' => NULL,
'type' => $node_type,
'uid' => $users[array_rand($users)],
'revision' => mt_rand(0, 1),
'status' => TRUE,
'promote' => mt_rand(0, 1),
'created' => REQUEST_TIME - mt_rand(0, $results['time_range']),
'langcode' => devel_generate_get_langcode($results),
);
if ($type->has_title) {
// We should not use the random function if the value is not random
if ($results['title_length'] < 2) {
$edit_node['title'] = devel_create_greeking(1, TRUE);
}
else {
$edit_node['title'] = devel_create_greeking(mt_rand(1, $results['title_length']), TRUE);
}
}
else {
$edit_node['title'] = '';
}
// #todo Remove once comment become field. http://drupal.org/node/731724
if (Drupal::moduleHandler()->moduleExists('comment')) {
$edit_node['comment'] = variable_get('comment_' . $node_type, COMMENT_NODE_OPEN);
}
$node = entity_create('node', $edit_node);
Using formatted text
Using grep with before/after code lines helped me figure out how to add a node with 'full_html'.
Search the Drupal core code with this :
$ cd drupal/core
$ grep -B 5 -A 5 -r entity_create.*node * > /tmp/temp-grep.txt
Then, open up /tmp/temp-grep.txt in a text editor. Poke around there a bit and you'll see this :
--
modules/editor/src/Tests/EditorFileUsageTest.php- $body_value .= '<img src="awesome-llama.jpg" data-editor-file-uuid="invalid-editor-file-uuid-value" />';
modules/editor/src/Tests/EditorFileUsageTest.php- // Test handling of a non-existing UUID.
modules/editor/src/Tests/EditorFileUsageTest.php- $body_value .= '<img src="awesome-llama.jpg" data-editor-file-uuid="30aac704-ba2c-40fc-b609-9ed121aa90f4" />';
modules/editor/src/Tests/EditorFileUsageTest.php- // Test editor_entity_insert(): increment.
modules/editor/src/Tests/EditorFileUsageTest.php- $this->createUser();
modules/editor/src/Tests/EditorFileUsageTest.php: $node = entity_create('node', array(
modules/editor/src/Tests/EditorFileUsageTest.php- 'type' => 'page',
modules/editor/src/Tests/EditorFileUsageTest.php- 'title' => 'test',
modules/editor/src/Tests/EditorFileUsageTest.php- 'body' => array(
modules/editor/src/Tests/EditorFileUsageTest.php- 'value' => $body_value,
modules/editor/src/Tests/EditorFileUsageTest.php- 'format' => 'filtered_html',
--
Note how 'body' now becomes an array with a 'value' and a 'format'.
Best way to create node in Drupal 8 via using core services
$node = \Drupal::entityTypeManager()->getStorage('node')->create([
'type' => 'content_type_machine_name',
'field_text' => 'Foo',
'title' => 'Text Title',
]);
$node->save();
Figured it out. For anyone else with this issue, nodes are now treated as entities, and the entity module is now part of core. So my code ended up looking like this:
$new_page_values = array();
$new_page_values['type'] = 'my_content_type';
$new_page_values['title'] = $form_state['values']['page_title'];
$new_page_values['path'] = $new_page_path;
$new_page = entity_create('node', $new_page_values);
$new_page->save();
I create a custom module and I want use the complete_url of my future website in the template who used/created by my module.
I tried several ways and searched Google but I don't find a solution.
However this issue seems very simply, that became me crazy x)
So, I must create a variable in my .module and add her when I return the array in the main_socialtag_theme function. Where/How can I do that ?
My .module:
function main_socialtag_block_info(){
$block['main_socialtag']=array(
'info' => t('Main socialtag'),
'cache' => DRUPAL_NO_CACHE,
);
return $block;
}
function main_socialtag_theme(){
return array(
'main_socialtag_block' => array(
'template' => 'theme/main_socialtag_block',
'variables' => array(),
),
);
}
function main_socialtag_block_view($delta=''){
if ($delta == 'main_socialtag'){
return array(
'subject' => '',
'content' => array(
'#theme' => 'main_socialtag_block',
)
);
}
}
Thanks for help, and sorry for my bad english writing !
If you are looking for a way to add, modify and call variables. Check variable_get and variable_set.
To add or modify a variable value:
variable_set("my_variable_name", "value");
To retrieve the value:
$myVal = variable_get("my_variable_name", "");
For hook_theme Implementation, kindly check this question.
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)