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
Related
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
I am trying to add some extra fields in theme my login Plugin Wordpress.
I am done with the documentation of the theme.
The extra fields aren't been saved on database. I have added the extra fields in the db as well but still the data are not been saved in db
Any Suggestion?
You need to use custom type not text and first_name
function add_tml_registration_form_fields() {
tml_add_form_field( 'register', 'custom_1', array(
'type' => 'custom',
'label' => 'First Name 1 ',
'value' => tml_get_request_value( 'first_name_test', 'post' ),
'id' => 'first_name_test',
'priority' => 15,
) );
tml_add_form_field( 'register', 'custom_2', array(
'type' => 'custom',
'label' => 'Last Name 1',
'value' => tml_get_request_value( 'last_name_test', 'post' ),
'id' => 'last_name_test',
'priority' => 15,
) );
}
add_action( 'init', 'add_tml_registration_form_fields' );
Read this
https://docs.thememylogin.com/article/104-function-tmladdformfield
<?php
function customtable_permission() {
return array(
'show people' => array(
'title' => t('List of people'),
'description' => t('The table'),
),
);
}
function customtable_menu() {
$items = array();
$items['people'] = array(
'type' => MENU_NORMAL_ITEM,
'title' => t('Title'),
'description' => 'This page should show a table from a remote DB',
'page callback' => 'customtable_db_data',
'access arguments' => array('show people'),
);
return $items;
}
function customtable_db_data() {
db_set_active('remote_database');
$results = db_query("SELECT * FROM {people}");
$header = array(t('Id'), t('Name'), t('Department'), t('Division'), t('Insurance'));
$rows = array();
foreach($results AS $result) {
$rows[] = array(
$result->id,
$result->name,
$result->department,
$result->division,
$result->insurance,
);
}
db_set_active('default');
return theme('table', array('header'=> $header, 'rows' => $rows));
}
?>
This all works fine and I can go to site.com/people and see the all the entries from the database printed nicely in a table
But I want text boxes where I can filter each column. Users can search by name or a specific insurance or department. I think it is possible programmatically, but I'd like to know if there is a more "drupal" approach. Content types have the ability to filter its fields. Am I to create a content type based on my query? I don't exactly know. Any assist is appreciated.
I think the best way to do this is migrate the query result to a drupal content type, to do this you need to use the migrate api.
-Install and enabled migrate and migrate_ui drupal modules.
-Create any content type you want with your fields.Using the drupal interface.
-Create a custom module, using migrate api. For example:
/sites/all/modules/custom/migrate_customtable/migrate_customtable.migrate.inc
function migrate_customtable_migrate_api() {
$api = array(
'api' => 2,
'groups' => array(
'custom_table' => array(
'title' => t('Custom Table'),
),
),
'migrations' => array(
'Projects' => array(
'class_name' => 'CustomTableMigration',
'group_name' => 'custom_table',
'event_log' => ''
),
),
);
return $api;
}
Then, create a class called: CustomTableMigration.inc that will contains the migration:
<?php
/**
* Created by PhpStorm.
* User: ldcontreras
* Date: 25/07/18
* Time: 10:13
*/
class CustomTableMigration extends Migration {
public function __construct($arguments) {
parent::__construct($arguments);
$query = Database::getConnection('default', 'migrate_custom_table')//this must be define in your settins.php file
->select('people')
->fields('productos_new', array(
'id',
'name',
'department',
'division',
'insurance',
)
);
$this->source = new MigrateSourceSQL($query, array(), NULL, array(map_joinable => FALSE));
$this->destination = new MigrateDestinationNode('content_type_machine_name'); //the content type created
$this->map = new MigrateSQLMap($this->machineName,
array(
'id' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => t('Source ID'),
)
),
MigrateDestinationNode::getKeySchema()
);
$this->addFieldMapping('title', 'name');
$this->addFieldMapping('field_department_content_type', 'department');
$this->addFieldMapping('field_division_content_type', 'division');
$this->addFieldMapping('field_insurance_content_type', 'insurance');
$this->addUnmigratedDestinations(array(
'body:format',
'comment',
'is_new',
'log',
'promote',
'revision',
'revision_uid',
'tnid',
'totalcount',
'daycount',
'timestamp',
'path',
'translate',
'sticky',
'uid',
));
}
}
Finally,enable your custom module and run the migration using drush.
$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)).
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