Drupal 8 views alter dynamic field value - drupal

I add multiple fields with
$view->addHandler($view->current_display, 'field', 'views', 'nothing', array(
'label' => 'My field',
'type' => 'textfield',
'alter' => array('text' => 'My field text'),
'element_class' => 'my-field',
'element_default_classes' => 0,
'group_rows' => TRUE
), 'my_field');
this works fine but i can't find a way to alter it, because the result data in pre_render, post_render, post_execute doesn't include these field only has entity fields which can be changed with
$result->_entity->set('title', 'newtitle')
i managed to change the field values eventually with template_preprocess_views_view_field() but that doesn't work for data export .csv / .xls
created a new views field in hook_views_data and a new plugin extending FieldPluginBase but that that doesn't work with addHandler.
is there a way to add dynamic fields and modify the output or create a new viewsfield and use it with $view->addHandler ?

have to create a new views field plugin and use that instead of nothing, and in the render function the value can be customized

Related

Output a "advanced-custom-fields"-field programmatically in wordpress backend

I created a custom field with the "advanced-custom-fields"-plugin. Now I want to get and output the custom field programmatically in my template file (backend, edit page), because my template is called via ajax if user want's to add a new region to the page.
Is there any function which returns the complete field? I only found functions which gave me values, but not the field as "form".
I found a solution. I duplicated the plugin folder into my theme root directory and put the following code into my functions.php:
function relationshipField() {
$newField = new acf_field_relationship();
$field = array(
'post_type' => array('post'),
'max' => '',
'taxonomy' => array('all'),
'filters' => array('search'),
'result_elements' => array('post_title', 'post_type'),
'return_format' => 'object'
);
return $newField->create_field($field);
}
In addition I append a custom input field (created in function.php) which stores only the post id's in database.

How to add a filter (like image rename) to a Theme Customization API setting

I want to add several image upload fields to my theme options by using the WP Customization API.
For each of these upload fields I want to rename the uploaded file to a fixed name.
So for example, I want to add a hero_image field, and it should always be stored as hero.jpg.
I added a setting called hero_image,
$wp_customize->add_setting( 'hero_image' , array(
'default' => '',
) );
This is the related control
$wp_customize->add_control(
new WP_Customize_Image_Control(
$wp_customize,
'hero_image_control',
array(
'label' => 'Site hero',
'section' => 'context_settings',
'settings' => 'hero_image',
'context' => 'your_setting_context'
)
)
);
I know I can add a sanitize callback, but this won't rename the uploaded file.
If anyone knows how to add a preupload filter for a specific field, it would make my day :D

Drupal login form customize

Hi i have used this code
<?php
$elements = drupal_get_form("user_login");
$form = drupal_render($elements);
echo $form;
?>
to get the default Drupal login form for my site but I need to customize the HTML, I have found some pages in module/users but did not understand how to customize the structure.
The user login form for Drupal is built by the user_login function in user.module using Drupal Form API. If you need to customize it, you should do it using hook_form_alter() in your module
function YOUR_MODULE_NAME_form_alter(&$form, &$form_state, $form_id) {
if ($form_id=='user_login') {
// YOUR CUSTOM CODE FOR THE FORM GOES HERE
}
}
** EDIT, AFTER YOUR COMMENT **
You don't need to call the YOUR_MODULE_NAME_form_alter() function: Drupal does that for you via the hook mechanism everytime it needs to build a form, and, when $form_id=='user_login', it modifies the login form to allow your customization. The way Drupal does that is discussed in detail in drupal.org, just follow the link I wrote at the beginning of this answer.
The user login form is declared this way in user.module:
// Display login form:
$form['name'] = array('#type' => 'textfield',
'#title' => t('Username'),
'#size' => 60,
'#maxlength' => USERNAME_MAX_LENGTH,
'#required' => TRUE,
);
$form['name']['#description'] = t('Enter your #s username.', array('#s' => variable_get('site_name', 'Drupal')));
$form['pass'] = array('#type' => 'password',
'#title' => t('Password'),
'#description' => t('Enter the password that accompanies your username.'),
'#required' => TRUE,
);
$form['#validate'] = user_login_default_validators();
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Log in'));
The $form array is passed by reference to your hook_form_alter() before being rendered, allowing for customization. So, let's say that you want to change the label of the textfield for the user name from "Username" to "Name of the User", you write
$form['name']['#title'] = t("Name of the User");
in your custom code. If you want to add another field to the form (a textarea, for example), you do
$form['otherfield'] = array(
'#title' => t('My new custom textarea'),
'#type' => 'textarea',
'#description' => t("A description of what this area is for"),
'#cols' => 10,
'#rows' => 3,
'#weight' => 20,
);
and Drupal will add the field to the user login form.
There are many different kind of fields and properties that you can customize this way: I encourage you to fully read the Form API documentation. This way you let Drupal take care of form generation, translation, rendering, validation and submission, also permitting to other modules to manipulate your form if needed.
I hope it's clear, have a good day.
use this in template.php
function themename_theme() {
$items = array();
$items['user_login'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'corporateclean') . '/templates',
'template' => 'user-login',
);
and create a template folder and within that create a file user-login.tpl.php and in this file you can put your html and could customize drupal login

Drupal 7, programmatically add field_image to entity

In Drupal 7 I created an entity programmatically. It has a few textfields added like name, price etc in the form:
$form['data']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => isset($model->data['name']) ? $model->data['name']: '',
'#maxlength' => 255,
'#required' => TRUE,
'#weight' => -3,
);
However, I'd like to be able to add multiple images to every entity it as well.
When I use Drupal's user interface to add fields, I can pick "Image: field_image (Image)" at the bottom, under "Add an existing field". This field has exactly the behaviour I want.
How can I add it programmatically to my entity?
You can just extend the content-type you made with the Drupal UI by using the HOOK_form_alter()-hook.
Or you can find out what the Array is generating when you make a multiple image-field by using a var_dump(); and add it to the Array of your custom made template.

Wysiwyg mo editor for Drupal 5 forms (Form API): How to enable it for several textareas?

I am trying to create a form with the Drupal 5 form API that has two textareas, both of which should have have a wysiwyg editor enabled to allow HTML formatted input. However, only the second textarea has the editor enabled, the other one displays the "Input format selector", but not the editor controls. I have tried this with TinyMCE 3.3.9.3 and 3.3.9.4b and CKEditor 3.5.1.6398 both using the wysiwyg module integration, the result in both cases is the same.
In this related question it is mentioned that there might be a problem of identical IDs. I have no clue how to transfer this solution to the Drupal Form API, since I gave the two fields different names. In the generated HTML, they have separate HTML ids based on the Drupal names I assigned.
The code I used to create the text areas is the following:
$form['oos'] = array(
'#tree' => false,
);
$form['oos']['oosmessage'] = array(
'#description' => t('Something'),
'#title' => t('Generic out of stock message'),
'#type' => 'textarea',
);
$form['oos']['format'] = filter_form(1, 20, array('format'));
$form['oosmd'] = array(
'#tree' => false,
);
$form['oosmd']['oosmessage_date'] = array(
'#type' => 'textarea',
'#title' => t('Out of stock message until a specific date'),
'#description' =>t('Something else.'),
);
$form['oosmd']['format'] = filter_form(1, 20, array('format'));
Thanks!
Ellen
Try to give the two textareas different ids and see if that works.

Resources