Bachir Soussi Chiadmi 1ccc5d07d6 coded the showroom field
still have to manage access
2016-11-26 21:18:06 +01:00

343 lines
11 KiB
Plaintext

<?php
// TODO: alter entity translation field permission with field_permission
// __ __ _ _______ __ __
// / / ____ _________ _/ /_(_)___ ____ / ____(_)__ / /___/ /
// / / / __ \/ ___/ __ `/ __/ / __ \/ __ \ / /_ / / _ \/ / __ /
// / /___/ /_/ / /__/ /_/ / /_/ / /_/ / / / / / __/ / / __/ / /_/ /
// /_____/\____/\___/\__,_/\__/_/\____/_/ /_/ /_/ /_/\___/_/\__,_/
/**
* Implements hook_field_info().
*
* Provides the description of the field.
*/
function materio_showroom_field_info() {
return array(
// We name our field as the associative name of the array.
'field_materio_showroom_location' => array(
'label' => t('Showroom Location'),
'description' => t('Define material location by showroom'),
'default_widget' => 'materio_showroom_location_text',
'default_formatter' => 'materio_showroom_location_simple_text',
),
);
// TODO: define in settings wich vocabulary is for showrooms
}
/**
* Implements hook_field_validate().
*
* This hook gives us a chance to validate content that's in our
* field. We're really only interested in the $items parameter, since
* it holds arrays representing content in the field we've defined.
* We want to verify that the items only contain RGB hex values like
* this: #RRGGBB. If the item validates, we do nothing. If it doesn't
* validate, we add our own error notification to the $errors parameter.
*
* @see field_example_field_widget_error()
*/
// function materio_showroom_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
// foreach ($items as $delta => $item) {
// // if (!empty($item['rgb'])) {
// // if (!preg_match('@^#[0-9a-f]{6}$@', $item['rgb'])) {
// // $errors[$field['field_name']][$langcode][$delta][] = array(
// // 'error' => 'field_example_invalid',
// // 'message' => t('Color must be in the HTML format #abcdef.'),
// // );
// // }
// // }
// }
// }
/**
* Implements hook_field_is_empty().
*
* hook_field_is_empty() is where Drupal asks us if this field is empty.
* Return TRUE if it does not contain data, FALSE if it does. This lets
* the form API flag an error when required fields are empty.
*/
function materio_showroom_field_is_empty($item, $field) {
// dsm($item,'item');
// dsm($field,'field');
return empty($item['location']) && empty($item['showroom_tid']);
}
// ______ __
// / ____/___ _________ ___ ____ _/ /____ __________
// / /_ / __ \/ ___/ __ `__ \/ __ `/ __/ _ \/ ___/ ___/
// / __/ / /_/ / / / / / / / / /_/ / /_/ __/ / (__ )
// /_/ \____/_/ /_/ /_/ /_/\__,_/\__/\___/_/ /____/
/**
* Implements hook_field_formatter_info().
*
* We need to tell Drupal that we have two different types of formatters
* for this field. One will change the text color, and the other will
* change the background color.
*
* @see field_example_field_formatter_view()
*/
function materio_showroom_field_formatter_info() {
return array(
// This formatter just displays the hex value in the color indicated.
'materio_showroom_location_simple_text' => array(
'label' => t('Simple text-based formatter'),
'field types' => array('field_example_rgb'),
),
// This formatter changes the background color of the content region.
// 'field_example_color_background' => array(
// 'label' => t('Change the background of the output text'),
// 'field types' => array('field_example_rgb'),
// ),
);
}
/**
* Implements hook_field_formatter_view().
*
* Two formatters are implemented.
* - field_example_simple_text just outputs markup indicating the color that
* was entered and uses an inline style to set the text color to that value.
* - field_example_color_background does the same but also changes the
* background color of div.region-content.
*
* @see field_example_field_formatter_info()
*/
function materio_showroom_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
// This formatter simply outputs the field as text and with a color.
case 'materio_showroom_location_simple_text':
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('@loc', array('@loc' => $item['location'])),
);
}
break;
}
return $element;
}
// _ ___ __ __
// | | / (_)___/ /___ ____ / /______
// | | /| / / / __ / __ `/ _ \/ __/ ___/
// | |/ |/ / / /_/ / /_/ / __/ /_(__ )
// |__/|__/_/\__,_/\__, /\___/\__/____/
// /____/
/**
* Implements hook_field_widget_info().
*
* Three widgets are provided.
* - A simple text-only widget where the user enters the '#ffffff'.
* - A 3-textfield widget that gathers the red, green, and blue values
* separately.
* - A farbtastic colorpicker widget that chooses the value graphically.
*
* These widget types will eventually show up in hook_field_widget_form,
* where we will have to flesh them out.
*
* @see field_example_field_widget_form()
*/
function materio_showroom_field_widget_info() {
return array(
'materio_showroom_location_text' => array(
'label' => t('Location as text'),
'field types' => array('field_materio_showroom_location'),
),
);
}
/**
* Implements hook_field_widget_form().
*
* hook_widget_form() is where Drupal tells us to create form elements for
* our field's widget.
*
* We provide one of three different forms, depending on the widget type of
* the Form API item provided.
*
* The 'field_example_colorpicker' and 'field_example_text' are essentially
* the same, but field_example_colorpicker adds a javascript colorpicker
* helper.
*
* field_example_3text displays three text fields, one each for red, green,
* and blue. However, the field type defines a single text column,
* rgb, which needs an HTML color spec. Define an element validate
* handler that converts our r, g, and b fields into a simulated single
* 'rgb' form element.
*/
function materio_showroom_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$locval = isset($items[$delta]['location']) ? $items[$delta]['location'] : '';
$showroom_tid = isset($items[$delta]['showroom_tid']) ? $items[$delta]['showroom_tid'] : 0;
$widget = $element;
$widget['#delta'] = $delta;
switch ($instance['widget']['type']) {
// TODO: loop through showrooms and don't allow more than one field by show room
case 'materio_showroom_location_text':
$widget['showroom_tid'] = array(
'#type' => 'hidden',
'#default_value' => $showroom_tid,
// Allow a slightly larger size that the field length to allow for some
// configurations where all characters won't fit in input field.
'#size' => 10,
'#maxlength' => 10,
);
$widget['location'] = array(
'#type' => 'textfield',
'#title' => "Showroom", //$showroom_term->name,
'#default_value' => $locval,
// Allow a slightly larger size that the field length to allow for some
// configurations where all characters won't fit in input field.
'#size' => 10,
'#maxlength' => 10,
);
break;
}
return $widget;
}
/**
* Implements hook_field_widget_settings_form().
*/
function materio_showroom_field_widget_settings_form($field, $instance) {
$widget = $instance['widget'];
$settings = $widget['settings'];
$vocs = taxonomy_get_vocabularies();
$options = array();
foreach ($vocs as $vid => $voc) {
$options[$vid] = $voc->name;
}
$form['vocabulary'] = array(
'#type' => 'select',
'#title' => t('Vocabulary'),
'#default_value' => $settings['vocabulary'],
// '#element_validate' => array('_tode_widget_settings_maxlength_validate'),
'#required' => TRUE,
'#description' => t('Choose which vocabulary will be associated as showroom.'),
'#options' => $options,
);
return $form;
}
/**
* Implements hook_form_alter().
*/
function materio_showroom_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
if($form['#field']['type'] == "field_materio_showroom_location"){
// dsm($form, 'form');
$form['field']['cardinality']['#disabled'] = 'true';
$form['field']['cardinality']['#default_value'] = -1;
}
//
}
/**
* Implements hook_form_alter().
*/
function materio_showroom_form_alter(&$form, &$form_state, $form_id) {
// act only on node edit form
if(isset($form['#node_edit_form'])){
// dsm($form_id, 'form_id');
// dsm($form, 'form');
// dsm($form_state, 'form_state');
// define some constants
$field_type = 'field_materio_showroom_location';
$node = $form['#node'];
$nodetype = $form['type']['#value'];
// get all (probably one :p) showroom field instances from the current node
$fieldsmap = field_info_field_map();
$showroomfieldinstances = array();
foreach ($fieldsmap as $field_name => $field) {
// dsm($field,$field_name);
if ($field['type'] == $field_type
&& isset($field['bundles']['node'])
&& in_array($nodetype, $field['bundles']['node'])) {
$showroomfieldinstances[] = $field_name;
}
}
// dsm($showroomfieldinstances, 'showroomfieldinstances');
if(!empty($showroomfieldinstances)){
// act on each field instance
foreach ($showroomfieldinstances as $field_name) {
// retrive various field infos
$field_info = field_info_field($field_name);
$field_instance = field_info_instance('node', $field_name, $nodetype);
// dsm($field_info, 'field_info');
// get all terms from chosen vocabulary in field instance widget settings
$vid = $field_instance['widget']['settings']['vocabulary'];
$tree = taxonomy_get_tree($vid);
foreach ($tree as $key => $term) {
$terms[$term->tid] = $term->name;
}
// dsm($terms, 'terms');
// get already recorded values
$old_items = field_get_items('node', $node, $field_name);
foreach ($old_items as $i => $value) {
$values[$value['showroom_tid']] = $value['location'];
}
// dsm($values, 'values');
// build new item list
foreach ($terms as $tid => $name) {
$items[] = array(
'showroom_tid' => $tid,
'location' => isset($values[$tid]) ? $values[$tid] : ''
);
}
// dsm($items, 'items');
// retrieve new field form with our custom items
$new_field_form = field_default_form('node', $node, $field_info, $field_instance, LANGUAGE_NONE, $items, $form, $form_state);
//dsm($new_field_form, 'default_form');
// change items location field title
$i = 0;
foreach ($terms as $tid => $name) {
$item = $new_field_form[$field_name][LANGUAGE_NONE][$i];
$new_field_form[$field_name][LANGUAGE_NONE][$i]['location']['#title'] = $terms[$item['showroom_tid']['#default_value']];
$i++;
}
// remove the last one more item added by default
unset($new_field_form[$field_name][LANGUAGE_NONE][$i]);
// delete normal field form and replace it with our new custom field form
unset($form[$field_name]);
$form[$field_name] = $new_field_form[$field_name];
// remove "add more" button
unset($form[$field_name]['und']['add_more']);
}
}
}
}