Drupal 8 - custom field plugin & specific taxonomy vocabulary - plugins

I'm building a custom compound field in Drupal 8. I'm almost there, but one final bit I'm missing: when I add this new field type to a content type, the taxonomy autocomplete field pulls from every taxonomy vocabulary on the site.
I'm trying to determine how to only pull from a specific "plant_parts" vocabulary. Currently, within my widget code, I've got this:
$element['plant_component_measured'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'taxonomy_term',
'#title' => t('Plant part'),
'#prefix' => '<table><tr><td>',
'#suffix' => ' ',
'#default_value' => isset($items[$delta]->plant_component_measured) ?
$items[$delta]->plant_component_measured : NULL,
);

You can use STATE or CONFIG API for saving the default value and specify your taxonomy type in #selection_settings['target_bundles'] = ['plant_part'] as below
use Drupal\taxonomy\Entity\Term;
:
:
if (empty(\Drupal::state()->get('YOUR_MODULE_NAME.plant_component_measured')))
\Drupal::state()->set('YOUR_MODULE_NAME.plant_component_measured', '');
$entity = Term::load(\Drupal::state()->get('YOUR_MODULE_NAME.plant_component_measured'));
$element['plant_component_measured'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'taxonomy_term',
'#title' => $this->t('Plant part'),
'#description' => $this->t('Plant part'),
'#prefix' => '<table><tr><td>',
'#suffix' => ' ',
'#default_value' => $entity,
'#selection_handler' => 'default',
'#selection_settings' => [
'target_bundles' => [
'plant_part'
],
],
];

Related

Prestashop fields_options in AdminMaintenanceController

In my override file AdminMaintenanceController, I try to add an input file to add an image(dynamically) on maintenance.tpl when the maintenance mode in active. The field form appear correctly in my backoffice below maintenance ip and switch but nothing is uploaded.
Do you have tips or informations about this issue ?
I'm on 1.6
My controller:
class AdminMaintenanceController extends AdminMaintenanceControllerCore
{
public function __construct()
{
$this->bootstrap = true;
$this->className = 'Configuration';
$this->table = 'configuration';
parent::__construct();
$this->fields_options = array(
'general' => array(
'title' => $this->l('General'),
'fields' => array(
'PS_SHOP_ENABLE' => array(
'title' => $this->l('Enable Shop'),
'desc' => $this->l('Activate or deactivate your shop (It is a good idea to deactivate your shop while you perform maintenance. Please note that the webservice will not be disabled).'),
'validation' => 'isBool',
'cast' => 'intval',
'type' => 'bool'
),
'PS_MAINTENANCE_IP' => array(
'title' => $this->l('Maintenance IP'),
'hint' => $this->l('IP addresses allowed to access the front office even if the shop is disabled. Please use a comma to separate them (e.g. 42.24.4.2,127.0.0.1,99.98.97.96)'),
'validation' => 'isGenericName',
'type' => 'maintenance_ip',
'default' => ''
),
),
'submit' => array('title' => $this->l('Save'))
),
'image' => array(
'title' => $this->l('Images parameters'),
'fields' => array(
'PS_IMG1' => array(
'title' => $this->l('Left side image'),
'type' => 'file',
'name' => 'PS_IMG1',
'thumb' => _PS_IMG_.'PS_IMG1.jpg',
'hint' => $this->l('Choose the photo for this side'),
),
),
'submit' => array('title' => $this->l('Save'))
),
It seems that you have implemented only a form fields display but not file(image) upload process. You need to instantiate a process of an image uploading after form confirmation

how can I add a dependant custom field through the manifest.php

I am currently adding a web hook and want to add some fields programmatically through the manifest.php.
I have found the relevent information to add fields as below:
'custom_fields' => array (
array (
'name' => 'text_c',
'label' => 'LBL_TEXT_C',
'type' => 'varchar',
'max_size' => 255,
'require_option' => 'optional',
'default_value' => '',
'ext1' => '',
'ext2' => '',
'ext3' => '',
'audited' => 1,
'module' => 'Accounts'
)
);
the issue is that I cant seem to find how to make the fields dependant i.e. they will only be visible if another field contains a specific value.
any help would be greatly appreciated
In the definition array you would need to add the dependency attribute like so :
'custom_fields' => array (
array (
'name' => 'text_c',
'label' => 'LBL_TEXT_C',
'type' => 'varchar',
'max_size' => 255,
'require_option' => 'optional',
'default_value' => '',
'ext1' => '',
'ext2' => '',
'ext3' => '',
'audited' => 1,
'module' => 'Accounts'
'dependency' => 'equal($other_field,"other field value")'
)
);
This would replicate setting the dependency option in Studio.
The dependency formula
'equal($other_field,"other field value")'
Means show this field when the another field other_field equals the string "other field value"

Prestashop Module : Add multi select dropdown

I'm working on a module and I would like to know how to add multiple dropdown with fields_options.
$this->fields_options = array(
'Test' => array(
'title' => $this->l('Test'),
'icon' => 'delivery',
'fields' => array(
'IXY_GALLERY_CREATION_OCCASION' => array(
'title' => $this->l('DropdownList'),
'type' => 'select',
'multiple' => true , // not working
'identifier' => 'value',
'list' => array(
1 => array('value' => 1, 'name' => $this->l('Test 1 ')),
2 => array('value' => 2, 'name' => $this->l('Test 2)'))
)
),
),
'description' =>'',
'submit' => array('title' => $this->l('Save'))
)
);
This is how I'm doin if you're meaning that :
$combo = $this->getAddFieldsValues();
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Title'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'select',
'lang' => true,
'label' => $this->l('Nom'),
'name' => 'nom_matiere',
'options' => array(
'query' => $combo[0],
'id' => 'id_option',
'name' => 'name'
)
),
array(
'type' => 'select',
'lang' => true,
'label' => $this->l('Nom'),
'name' => 'name',
'options' => array(
'query' => $combo[1],
'id' => 'id_option',
'name' => 'name'
)
),
),
),
'submit' => array(
'title' => $this->l('Save'),
'name' => $this->l('updateData'),
)
),
);
the answer are not correctly .. due its not only dfined the field in the database, also must capture and stored in special way the values, in this example i demostrate to store as "1,2,3,6,8" using a single field
THE COMPLETE CODE AND ALL THE STEPS ARE AT: https://groups.google.com/forum/m/?hl=es#!topic/venenuxsarisari/z8vfPsvFFjk
here i put only the most important parts..
as mentioned int he previous link, added a new fiel in the model definition, class and the table sql
this method permits to stored in the db as "1,2,3" so you can use only a single field to relation that multiple selected values, a better could be using groupbox but its quite difficult, take a look to the AdminCustomers controller class in the controllers directory of the prestachop, this has a multiselect group that used a relational table event stored in single field
then in the helper form list array of inputs define a select as:
at the begining dont foget to added that line:
// aqui el truco de guardar el multiselect como una secuencia separada por comas, mejor es serializada pero bueh
$this->fields_value['id_employee[]'] = explode(',',$obj->id_employee);
this $obj are the representation of the loaded previous stored value when go to edit ... from that object, get the stored value of the field of your multiselect, stored as "1,3,4,6"
and the in the field form helper list of inputs define the select multiple as:
array(
'type' => 'select',
'label' => $this->l('Select and employee'),
'name' => 'id_employee_tech',
'required' => false,
'col' => '6',
'default_value' => (int)Tools::getValue('id_employee_tech'),
'options' => array(
'query' => Employee::getEmployees(true), // el true es que solo los que estan activos
'id' => 'id_employee',
'name' => 'firstname',
'default' => array(
'value' => '',
'label' => $this->l('ninguno')
)
)
),
an then override the post process too
public function postProcess()
{
if (Tools::isSubmit('submitTallerOrden'))
{
$_POST['id_employee'] = implode(',', Tools::getValue('id_employee'));
}
parent::postProcess();
}
this make stored in the db as "1,2,3"

Drupal form API checkboxes hierarchy

I am trying to setup a configuration page consisting of hierarchical checkboxes like the following:
-Parent#1
--option#1
--option#2
-Parent#2
--option#1
--option#2
To achieve the above layout, I am using the following code:
$form['categories']['templates']['parent1'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the appropriate actions for the form based on the indicated template'),
'#options' => array("Parent#1"),
'#multiple' => TRUE,
);
$form['categories']['templates']['parent1']['actions'] = array(
'#type' => 'checkboxes',
'#options' => array("P1 - option#1", "P1 - option#2"),
'#multiple' => TRUE,
);
$form['categories']['templates']['parent2'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the appropriate actions for the form based on the indicated template'),
'#options' => array("Parent#2"),
'#multiple' => TRUE,
);
$form['categories']['templates']['parent2']['actions'] = array(
'#type' => 'checkboxes',
'#options' => array("P2 - option#1", "P2 - option#2"),
'#multiple' => TRUE,
);
But the effect I am getting to not as desired. I attached an image of what the code is generating:
You can use the new Form API feature, #states to achieve this.
Note that, for ease of use, I'm wrapping the conditional checkboxes with a fieldset.
<?php
$form['categories']['templates']['parent1'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the appropriate actions for the form based on the indicated template'),
'#options' => array(1 => 'Parent#1'), // Note this! You can't use 0 as a key in #options.
'#multiple' => TRUE,
);
$form['categories']['templates']['parent1']['wrapper'] = array(
'#type' => 'fieldset',
'#title' => t('Options for #option', array('#option' => 'Parent#1')),
'#states' => array(
'visible' => array(
':input[name="parent1[1]"]' => array('checked' => TRUE), // We use "parent1" and its Nth option. in above field, See #options in above checkboxes field.
),
),
);
$form['categories']['templates']['parent1']['wrapper']['actions'] = array(
'#type' => 'checkboxes',
'#options' => array("P1 - option#1", "P1 - option#2"), // This is also wrong. You must define a key => value pair for #options or somehow avoid using 0 as the key.
'#multiple' => TRUE,
);
?>
You might want to use #tree => TRUE in the fieldsets to avoid Drupal from merging same keys' values together.
Also, you won't need #multiple in checkboxes field type.
Update
Example with node types:
<?php
$node_types = node_type_get_names();
$form['categories']['templates']['parent1'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the appropriate actions for the form based on the indicated template'),
'#options' => $node_types,
);
foreach ($node_types as $type => $label) {
$form['categories']['templates']['node-options'.$type] = array(
'#type' => 'checkboxes',
'#title' => t('Options for #type', array('#type' => $label)),
'#options' => array("Parent#1"),
'#multiple' => TRUE,
'#states' => array(
'visible' => array(
':input[name="parent1['.$type.']"]' => array('checked' => TRUE), // We use "parent1" and its Nth option. in above field, See #options in above checkboxes field.
),
),
);
}

Insert email when creating account with rest api

I am faced with a Sugar 6.3 CE installation, trying to create new accounts through rest api.
I am still on the learning curve for a lot of the innards of the CRM, and cannot figure out how to have the email inserted whith the rest of the account info upon creation with a REST call.
I have tried many different field values, and after catching that $email1 was used in some snippet examples I saw on the sugarCRM site. I have not found other mentions in the forums or in the docs yet.
The $parameters array used to configure the usual rest call to create an account in php with REST api looks like this and works fine, except for the $email1:
$parameters = array(
'session' => $session,
'module' => 'Contacts',
'name_value_list' => array(
array('name' => 'first_name', 'value' =>
utf8_encode($contacts["Name"])),
array('name' => 'last_name', 'value' =>
utf8_encode($contacts["GivenName"])),
array('name' => 'phone_work', 'value' =>
utf8_encode($row->PrimaryPhoneAreaCode . ' ' . $row->PrimaryPhone)),
array('name' => 'phone_fax', 'value' =>
utf8_encode($row->PrimaryFaxAreaCode . ' ' . $row->PrimaryFaxNumber)),
array('name' => 'title', 'value' =>
utf8_encode($contacts["Title"])),
/*
* PROBLEM HERE!
*/
array('name' => 'email1', 'value' =>
utf8_encode($row->PrimaryEmail)),
array('name' => 'primary_address_street', 'value' =>
utf8_encode($row->Address1) . ' ' .
utf8_encode($row->Address2)),
array('name' => 'language', 'value' =>
utf8_encode($row->Language)),
array('name' => 'assigned_user_id', 'value' =>
get_rep_id($row->Salesperson1Name, $sugarlink)),
)
);
I would be curious, if someone has the trick. I tried to find to field for emails but it seems to be in separate tables. Any help / tips appreciated.
If you are using REST API the email1 will work for both set en get methods (just tested it).
Did not used the SOAP API as in your example, and suggest to migrate all to REST according to SugarCRM recommendations.
You must add the email into emailadress database first, create contact and set a relationship between both ;-)
$set_entry_parametersEADDR = array(
"session" => $session_id,
//The name of the module from which to retrieve records.
"module_name" => "EmailAddresses",
//Record attributes
"name_value_list" => array(
array('name' => 'email_address', 'value' => $email),
array('name' => 'email_address_caps', 'value' => strtoupper($email)),
array('name' => 'invalid_email' , 'value' => 0),
array('name' => 'opt_out', 'value' => 0),
array('name' => 'date_created' , 'value' => date('Y-m-d H:i:s')),
array('name' => 'date_modified', 'value' => date('Y-m-d H:i:s')),
array('name' => 'deleted' , 'value' => 0),
),
);
$set_entry_resultEmailsAdd = call("set_entry", $set_entry_parametersEADDR, $url);
print_r($set_entry_resultEmailsAdd);
$Email_id = $set_entry_resultEmailsAdd->id;
$set_entry_parameters_contact = array(
//session id
"session" => $session_id,
//The name of the module from which to retrieve records.
"module_name" => "Contacts",
//Record attributes
"name_value_list" => array(
//to update a record, you will nee to pass in a record id as commented below
//array("name" => "id", "value" => "9b170af9-3080-e22b-fbc1-4fea74def88f"),
array("name" => "first_name", "value" => $prof["Prenom"]),
array("name" => "last_name", "value" => $prof["Nom"]),
array("name" => "login_c", "value" => $prof["Login"]),
//array("name" => "email1", "value" => $email),
array("name" => "fonction_c", "value" => "prof")
),
);
$set_entry_result_contact = call("set_entry", $set_entry_parameters_contact, $url);
print_r($set_entry_result_contact);
$contact_id = $set_entry_result_contact->id;
$set_relationship_parameters_email = array(
'session' => $session_id,
'module_name' => 'Contacts',
'module_id' => $contact_id,
'link_field_name' => 'email_addresses',
'related_ids' => $Email_id,
);
$set_relationship_result_email = call("set_relationship", $set_relationship_parameters_email, $url);
print_r($set_relationship_result_email);
Currently I think that not works with REST api but works with SOAP API. Could you try to use email1_set_in_workflow key instead of email1?
It's not a very good solution but perhaps that could unlock you pending a better way to do that in future release