set default value for custom field type: list_boolean / options_onoff - drupal

$instance = array(
'field_name' => $field_name,
'entity_type' => $entity,
'bundle' => $bundle,
'field types' => 'list_boolean',
'widget' => array(
'type' => 'options_onoff',
'settings' => array('display_label' => 1)
),
'default_value' => array(array('value' => 1)),
);
this is not taken, and i have to save it twice in the admin contenttype - field/edit,
until it takes it ...

i now exported the finished field with the features module,
and took the generated code - suddenly it works, with default_value
i guess i was missing the property module on the field, also field types is inexistant ..

In your field definition, you have to set the allowed_values in the settings array in order for the default_value in the instance to get picked up.
so like this assuming you are doing this in a module
$fields[] = array(
'field_name' => '$field_name',
'type' => 'list_boolean',
'settings' => array(
'allowed_values' => drupal_map_assoc(range(0, 1)),
),
);

Instead of using 'default_value', I got it to work by using 'default_value_function' and creating a function that returns array(array('value' => 1)).

Related

How to extend sorting order of Team Members

I'm using Business Lounge theme by RT themes with Elementor.
Wordpress version is current (5.2.1)
On the team page (Demo: https://businesslounge-demo.rtthemes.com/our-team/) there is a list of cards of team members. I want to change the order of the team members to an option that is not currently selectable.
The team member list is done with a shortcode [staff_box]
In Elementor edit mode I looks like this:
Edit:
The edit form is defined in
wp-content/plugins/businesslounge-extensions/inc/elementor-addons/staff.php
<?php
namespace Elementor;
// ...
class Widget_RT_Staff extends Widget_Base {
// ...
protected function _register_controls() {
// ...
$this->add_control(
'list_orderby',
[
'label' => esc_html_x( 'List Order By', 'Admin Panel','businesslounge' ),
'description' => esc_html_x('Sorts the posts by this parameter', 'Admin Panel','businesslounge' ),
'type' => Controls_Manager::SELECT,
'default' => "date",
"options" => array(
'date' => esc_html_x('Date',"Admin Panel","businesslounge"),
'author' => esc_html_x('Author',"Admin Panel","businesslounge"),
'title' => esc_html_x('Title',"Admin Panel","businesslounge"),
'modified' => esc_html_x('Modified',"Admin Panel","businesslounge"),
'ID' => esc_html_x('ID',"Admin Panel","businesslounge"),
'rand' => esc_html_x('Randomized',"Admin Panel","businesslounge"),
)
]
);
// ...
}
// ...
}
Plugin::instance()->widgets_manager->register_widget_type( new Widget_RT_Staff() );
The edit form is defined in `wp-content/plugins/businesslounge-extensions/inc/editor/staff_box.php`
like so
<?php
vc_map(
array(
'base' => 'staff_box',
'name' => _x( 'Team', 'Admin Panel','businesslounge' ),
'icon' => 'rt_theme rt_team',
'category' => array(_x( 'Content', 'Admin Panel','businesslounge' ), _x( 'Theme Addons', 'Admin Panel','businesslounge' )),
'description' => _x( 'Displays team members', 'Admin Panel','businesslounge' ),
'params' => array(
// ...
array(
'param_name' => 'list_orderby',
'heading' => _x( 'List Order By', 'Admin Panel','businesslounge' ),
"description" => _x("Sorts the posts by this parameter",'Admin Panel','businesslounge'),
'type' => 'dropdown',
"value" => array(
_x('Date','Admin Panel','businesslounge') => 'date',
_x('Author','Admin Panel','businesslounge') => 'author',
_x('Title','Admin Panel','businesslounge') => 'title',
_x('Modified','Admin Panel','businesslounge') => 'modified',
_x('ID','Admin Panel','businesslounge') => 'ID',
_x('Randomized','Admin Panel','businesslounge') => 'rand',
),
'save_always' => true
),
// ...
The output is defined in
wp-content/plugins/businesslounge-extensions/inc/shortcodes/staff_box.php
like so:
<?php
function rt_staff( $atts, $content = null ) {
// ...
//defaults
extract(shortcode_atts(array(
"id" => 'staff-'.rand(100000, 1000000),
"class" => "",
"list_layout" => "1/1",
"list_orderby" => "date",
"list_order" => "DESC",
"ids" => array(),
"box_style" => ""
), $atts));
// ...
//general query
$args=array(
'post_status' => 'publish',
'post_type' => 'staff',
'orderby' => $list_orderby,
'order' => $list_order,
'showposts' => 1000
);
// ...
$theQuery = query_posts($args);
// ...
What I want to do:
Add an option 'post_name' to the select box so that I can sort the team by a different field. I want to add
'Post name' => 'post_name',
How can I do this without changing the original source code?
I already have the child theme of business_lounge theme activated.
Do I need a custom extension for this?
You'd have to modify the original code.
You can't override the function unless the author applied a filter to the value returned, or used an action.
As gael notes in his answer, you can soften the blow of losing changes on updates by copying the original code into your child theme's functions.php then renaming the function - for e.g. to my_rt_staff() before adding in your modifications.
You would however still need to call the my_rt_staff() in the plugin instead of rt_stuff and you would have to make this change whenever the plugin was updated, but you wouldn't lose your code.
(Perhaps you could change the "list_orderby" => "date" in the default shortcode attributes to "list_orderby" => "post_name", as default in your modified my_rt_staff() method so it orders by name as default instead of date)
However, this does not help much in your specific circumstance, as the ideal modification you need to make is to the control itself, on the _register_controls() method in the Widget_RT_Staff class. You can override this by extending Widget_RT_Staff but you would still need to call your new class which results in you modifying the plugin code.
Without seeing how the Widget_RT_Staff class affects the shortocde, I can't be certain this would work, but based on the rt_staff() method, if you use the shortcode as [staff_box orderby="post_name"] you may get your intended result without having to touch any code.
You should be able to modify your plugin without losing update abilities by 'overriding' the function as described here:
https://stackoverflow.com/a/40856197/3623080
Make a copy of the function in your child-theme's functions.php, customize and rename it, use it in place of the original function.
Hope it will help

New fields aren't created after updating a custom module. DRUPAL 7

I have a custom module on my site. I try to install an update with a new field for my vocabulary, but the field doesn't appear.
hook_update:
function mymodule_update_7118()
{
$field_name = 'field_newfield';
if ( field_info_field( $field_name ) ) {
return;
}
$field = array(
'field_name' => $field_name,
'type' => 'list_integer',
'settings' => array(
'allowed_values' => array(
'Yes' => 1, //heard that adding a NO value may cause problems, although it doesn't work with a no value either.
),
),
);
$field = field_create_field( $field );
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => 'taxonomy',
'bundle' => 'vocab_name',
'label' => 'Label',
'widget' => array(
'active' => 1,
'module' => 'options',
'settings' => array(),
'type' => 'options_select',
'weight' => '3',
),
);
field_create_instance($instance);
}
Logs contain several recordings of Internalization module creating a string to translate this field. Also all needed tables are created in the database, but they are all empty.
For creating a new custom field you must do it like a custom module. The steps can be found out at https://drupal.stackexchange.com/questions/140517/how-to-create-new-field-type
You can find the excellent field_example module from the Examples Module which is always the first place to look. Examples module can be downloaded from https://www.drupal.org/project/examples

Drupal 7: Make custom content translatable

I'm creating a custom module which enables me to add Countries to a custom table in the database. I'll do later more with it, but as I got stuck at the beginning, I can't go on.
First my piece of code:
function partners_schema()
{
$schema['partners_country'] = array(
'description' => 'TODO: please describe this table!',
'fields' => array(
'id' => array(
'description' => 'auto inc id of country',
'type' => 'serial',
'not null' => true,
),
'name' => array(
'description' => 'name of country',
'type' => 'varchar',
'length' => '255',
'not null' => true,
'translatable' => true,
),
'needDistributor' => array(
'description' => 'is a distributor needed',
'type' => 'int',
'size' => 'tiny',
'not null' => true,
),
),
'primary key' => array('id'),
);
return $schema;
}
The code above generates my Database table. After searching I found that I can add 'translatable' => true to my schema, so Drupal knows that this field is translatable content.
I've added a form, to insert data into that schema, but now I got stuck. What do I have to do, that the user is able to translate the column name?
Thanks for your help.
Have a look at the accepted answer on this post it should help clear a few things up.

Correct way to use Drupal 7 Entities and Field API

I'm trying to use Drupal 7's entities and field API to correctly build a new module. What I have been unable to understand from the documentation is the correct way to use the new API to create a 'content type' (not a node type) with a number of set fields, such as Body.
I'm trying to set up the entity using hook_entity_info, then I believe I need to add the body field using field_create_instance, but I can't seem to get it to work.
In mycontenttype.module:
/**
* Implements hook_entity_info().
*/
function mycontenttype_entity_info() {
$return = array(
'mycontenttype' => array(
'label' => t('My Content Type'),
'controller class' => 'MyContentTypeEntityController',
'base table' => 'content_type',
'uri callback' => 'content_type_uri',
'entity keys' => array(
'id' => 'cid',
'label' => 'title',
),
'bundles' => array(
'mycontenttype' => array(
'label' => 'My Content Type',
'admin' => array(
'path' => 'admin/contenttype',
'access arguments' => array('administer contenttype'),
),
),
),
'fieldable' => true,
),
);
return $return;
}
/**
* Implements hook_field_extra_fields().
*/
function mycontenttype_field_extra_fields() {
$return['mycontenttype']['mycontenttype'] = array(
'form' => array(
'body' => array(
'label' => 'Body',
'description' => t('Body content'),
'weight' => 0,
),
),
);
return $return;
}
Then does this go in the .install file?
function mycontenttype_install() {
$field = array(
'field_name' => 'body',
'type' => 'text_with_summary',
'entity_types' => array('survey'),
'translatable' => TRUE,
);
field_create_field($field);
$instance = array(
'entity_type' => 'mycontenttype',
'field_name' => 'body',
'bundle' => 'mycontenttype',
'label' => 'Body',
'widget_type' => 'text_textarea_with_summary',
'settings' => array('display_summary' => TRUE),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'text_default',
),
'teaser' => array(
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
),
),
);
field_create_instance($instance);
}
I think your problem is that if node module is installed, there is already a field named 'body'. You should either re-name your field to something like 'mycontenttype_body' (comment.module uses comment_body), or re-use the 'body' field and skip the adding the field part and skip to adding the instance of it. The former is recommended over the latter.
Every field has an array property, entity_types, which limits the entities to which the field can be attached.
The best Drupal solution I can find, hook_field_create_field, can alter fields as they are created, but that's no good for the body field which is created on installation.
So my solution is just to edit the database directly in my hook_install
$data_col = db_query("SELECT data from field_config where field_name = 'body'")->fetchAssoc();
$data = unserialize($data_col['data']);
$data['entity_types'][] = 'MY_ENTITY_TYPE';
db_update('field_config')
->fields(array('data' => array('data' => serialize($data))))
->condition('field_name', 'body')
->execute();
just started down the same path here is a video from fago
Here's a nice repo to start: Lawmakers entity

Managing several custom content types from one module(drupal)

Is it possible to declare and manage several custom content types inside one module? I'm creating a site that needs four custom content types and I'd like to manage them from one module instead of creating module for every content type. After some testing, I found out that it seems impossible. Because, unless hook_form and content type share the same name of module, drupal doesn't call hook_form.
Here's how I'd like to do -
function mycontent_node_info(){
return array(
'mycontent1' => array(
'name' => t('....'),
'module' => 'mycontent',
'description' => t('...),
'has_title' => TRUE,
'title_label' => t('Title'),
'has_body' => TRUE,
'body_label' => t('content body'),
),
'mycontent2' => array(
.......
),
'mycontent3' => array(
......
),
'mycontent4' => array(
......
),
);
}
function mycontent1_form(&$node){
$form['control1'] = array(
'#type' => 'select',
'#options' => array(
'0' => t('selection 1'),
'1' => t('selection 2'),
),
'#attributes' => array('id'=>'control1'),
);
$form['control2'] = array(
'#type' => 'select',
'#options' => array(
'0' => t('1'),
'1' => t('2'),
'2' => t('3'),
'3' => t('4'),
),
'#attributes' => array('id'=>'control2'),
);
return $form;
}
function mycontent2_form(&$node){
....
}
function mycontent3_form(&$node){
....
}
function mycontent4_form(&$node){
....
}
Am I doing something wrong here or is not possible and there's no alternative other than creating module for every content types. I appreciate much your help.
The prefix for all your hooks should be the name of your module, i.e. mycontent_node_info() and mycontent_form(&$node). I think the content type itself can be called whatever you want but by convention anything global that you define in a module should be prefixed with the name of the module to avoid namespace problems. So your content becomes mycontent_type1, mycontent_type2, etc... As for dealing with hook_form, I guess the way to do it is to check the type of the node passed in and act accordingly.
You might try using the Features module (http://drupal.org/project/features) to export your content types. It auto generates the code to make this work, and you can have a look at what is going wrong with your code.

Resources