123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- <?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');
- _materio_showroom_alter_location_field_form($form, $form_state, $form_id);
- }
- }
- function _materio_showroom_alter_location_field_form(&$form, &$form_state, $form_id){
- // define some constants
- $field_type = 'field_materio_showroom_location';
- $node = $form['#node'];
- $nodetype = $form['type']['#value'];
- global $user;
- $user = user_load($user->uid); // Make sure the user object is fully loaded
- // dsm($user, 'user');
- $fieldsmap = field_info_field_map();
- $showroomfieldinstances = array();
- $user_termref_field_instances = array();
- foreach ($fieldsmap as $field_name => $field) {
- // dsm($field,$field_name);
- // get all (probably one :p) showroom field instances from the current node
- if ($field['type'] == $field_type
- && isset($field['bundles']['node'])
- && in_array($nodetype, $field['bundles']['node'])) {
- $showroomfieldinstances[] = $field_name;
- }
- // get all term reference fields instance from users
- if (isset($field['bundles']['user'])
- && $field['type'] == 'taxonomy_term_reference') {
- $field_info = field_info_field($field_name);
- // dsm($field_info, "field_info");
- $user_termref_field_instances[$field_info['settings']['allowed_values'][0]['vocabulary']] = $field_name;
- }
- }
- // dsm($showroomfieldinstances, 'showroomfieldinstances');
- // dsm($user_termref_field_instances, 'user_termref_field_instances');
- // if there is no showroom field instances do nothing
- 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'];
- $voc = taxonomy_vocabulary_load($vid);
- $tree = taxonomy_get_tree($vid);
- foreach ($tree as $key => $term) {
- $terms[$term->tid] = $term->name;
- }
- // dsm($terms, 'terms');
- // get user own term for current vocabulary (if any)
- if(isset($user_termref_field_instances[$voc->machine_name])){
- $user_field_name = $user_termref_field_instances[$voc->machine_name];
- foreach (field_get_items('user', $user, $user_field_name) as $key => $value) {
- $user_showrooms[] = $value['tid'];
- }
- // dsm($user_showrooms, "user_showrooms");
- }
- // 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
- // and check access comparing term id with user taged with
- $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']];
- $new_field_form[$field_name][LANGUAGE_NONE][$i]['location']['#disabled'] = !in_array($tid, $user_showrooms);
- $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']);
- }
- }
- }
- // TODO: migrate old location field to new one
|