Skip to content

Instantly share code, notes, and snippets.

@cyberlex404
Created April 4, 2017 15:55
Show Gist options
  • Select an option

  • Save cyberlex404/72e275e7c2e92336946cd6a48be076e2 to your computer and use it in GitHub Desktop.

Select an option

Save cyberlex404/72e275e7c2e92336946cd6a48be076e2 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* An example field using the Field Types API.
*/
/**
* @defgroup field_example Example: Field Types API
* @ingroup examples
* @{
* Examples using Field Types API.
*
* This is updated from Barry Jaspan's presentation at Drupalcon Paris,
* @link http://acquia.com/community/resources/acquia-tv/intro-field-api-module-developers Video Presentation @endlink
*
* Providing a field requires:
* - Defining a field:
* - hook_field_info()
* - hook_field_schema()
* - hook_field_validate()
* - hook_field_is_empty()
*
* - Defining a formatter for the field (the portion that outputs the field for
* display):
* - hook_field_formatter_info()
* - hook_field_formatter_view()
*
* - Defining a widget for the edit form:
* - hook_field_widget_info()
* - hook_field_widget_form()
*
* Our module defines the field in field_example_field_info(),
* field_example_field_validate() and field_example_field_is_empty().
* field_example_field_schema() is implemented in field_example.install.
*
* Our module sets up a formatter in field_example_field_formatter_info() and
* field_example_field_formatter_view(). These are the API hooks that present
* formatted and themed output to the user.
*
* And finally, our module defines the widget in
* field_example_field_widget_info() and field_example_field_widget_form().
* The widget is the form element used to receive input from the user
* when the field is being populated.
*
* @see field_types
* @see field
*/
/***************************************************************
* Field Type API hooks
* bras_field_phone
***************************************************************/
/**
* Implements hook_field_info().
*
* Provides the description of the field.
*/
function bras_field_phone_field_info() {
return array(
// We name our field as the associative name of the array.
'bras_field_phone_with_tags' => array(
'label' => t('Phone number'),
'description' => t('Adds a phone number with tags: operator, viber, whatsapp'),
'default_widget' => 'bras_field_phone_with_tags_widget',
'default_formatter' => 'bras_field_phone_with_tags_formatter',
),
);
}
/**
* 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 bras_field_phone_field_widget_error()
*/
function bras_field_phone_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
$pattern = '/^((\+\d{1,4})[\- ]?)?(\(?\d{2,3}\)?[\- ]?)?[\d\- ]{7,10}$/';
foreach ($items as $delta => $item) {
// $m= preg_match($pattern, $item['phone'], $matches);
// dpm($matches, $m);
if (!empty($item['phone'])) {
if (!preg_match($pattern, $item['phone'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'bras_field_phone_invalid',
'message' => 'Номер не соответствует формату',
);
}
}
}
}
/**
* Implements hook_field_is_empty().
*
* hook_field_is_emtpy() 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 bras_field_phone_field_is_empty($item, $field) {
return empty($item['phone']);
}
/**
* 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 bras_field_phone_field_formatter_view()
*/
function bras_field_phone_field_formatter_info() {
return array(
// This formatter just displays the hex value in the color indicated.
'bras_field_phone_with_tags_formatter' => array(
'label' => t('Phone number with icons'),
'field types' => array('bras_field_phone_with_tags'),
),
// This formatter changes the background color of the content region
'bras_field_phone_number_only' => array(
'label' => t('Phone number only'),
'field types' => array('bras_field_phone_with_tags'),
),
// bras_field_phone_number_operator_only
'bras_field_phone_number_operator_only' => array(
'label' => t('Phone number operator only'),
'field types' => array('bras_field_phone_with_tags'),
),
);
}
/**
* Implements hook_field_formatter_view().
*
* Two formatters are implemented.
* - bras_field_phone_simple_text just outputs markup indicating the color that
* was entered and uses an inline style to set the text color to that value.
* - bras_field_phone_color_background does the same but also changes the
* background color of div.region-content.
*
* @see bras_field_phone_field_formatter_info()
*/
function bras_field_phone_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
// $nid = $entity->nid;
// dpm($entity);
// dpm($field, '$field');
// dpm($instance, '$instance');
$module_path = drupal_get_path('module','bras_field_phone');
//drupal_add_js($module_path . '/hous-phones.js');
$nid = isset($entity->nid) ? $entity->nid : $entity->id;
switch ($display['type']) {
// This formatter simply outputs the field as text and with a color.
case 'bras_field_phone_with_tags_formatter':
foreach ($items as $delta => $item) {
$operator = _bras_field_phone_ico_content($item['operator']);
$viber = $item['viber'] ? _bras_field_phone_ico_content('viber') : '';
$whatsapp = $item['whatsapp'] ? _bras_field_phone_ico_content('whatsapp') : '';
$element[$delta] = array(
'#type' => 'markup',
'#prefix' => '<span class="housing-phone-item hous-phone-' . $nid . '">',
'#suffix' => '</span>',
'#markup' => '<span itemprop="telephone" housing="' . $nid . '">' . $item['phone'] . '</span><sup class="icons">' . $operator . $viber . $whatsapp . '</sup>',
);
}
break;
case 'bras_field_phone_number_only':
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#type' => 'markup',
'#markup' => '<span itemprop="telephone">' . $item['phone'] . '</span>',
);
}
break;
case 'bras_field_phone_number_operator_only':
foreach ($items as $delta => $item) {
$operator = _bras_field_phone_ico_content($item['operator']);
$element[$delta] = array(
'#type' => 'markup',
'#markup' => '<span itemprop="telephone">' . $item['phone'] . '</span><sup class="icons">' . $operator . '</sup>',
);
}
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 bras_field_phone_field_widget_form()
*/
function bras_field_phone_field_widget_info() {
return array(
'bras_field_phone_with_tags_widget' => array(
'label' => t('Phone with tags icons'),
'field types' => array('bras_field_phone_with_tags'),
),
/*
'bras_field_phone_3text' => array(
'label' => t('RGB text field'),
'field types' => array('bras_field_phone_rgb'),
),
'bras_field_phone_colorpicker' => array(
'label' => t('Color Picker'),
'field types' => array('bras_field_phone_rgb'),
),
*/
);
}
/**
* 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 'bras_field_phone_colorpicker' and 'bras_field_phone_text' are essentially
* the same, but bras_field_phone_colorpicker adds a javascript colorpicker
* helper.
*
* bras_field_phone_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 bras_field_phone_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$phone_value = isset($items[$delta]['phone']) ? $items[$delta]['phone'] : '';
$operator_value = isset($items[$delta]['operator']) ? $items[$delta]['operator'] : 'none';
$viber_value = isset($items[$delta]['viber']) ? $items[$delta]['viber'] : '';
$whatsapp_value = isset($items[$delta]['whatsapp']) ? $items[$delta]['whatsapp'] : '';
$element += array(
'#delta' => $delta,
);
$element['phone'] = array();
$element['operator'] = array();
$element['viber'] = array();
$element['whatsapp'] = array();
switch (
$instance['widget']['type']){
case 'bras_field_phone_with_tags_widget':
// текстовое поля для контакта
$element['phone'] += array(
'#title' => t('Enter phone'),
'#type' => 'textfield',
'#default_value' => $phone_value,
'#size' => 20,
'#maxlength' => 60,
);
// селект для выбора типа
$element['operator'] += array(
'#title' => t('Operator'),
'#type' => 'select',
'#default_value' => $operator_value,
'#options' => array(
'none' => t('None'),
'mts' => t('MTS'),
'velcom' => t('Velcom'),
'life' => t('Life :)'),
// 'other' => t('Other'),
),
);
$element['viber'] += array(
'#title' => t('Select is viber'),
'#type' => 'checkbox',
'#default_value' => $viber_value,
);
$element['whatsapp'] += array(
'#title' => t('Select is whatsapp'),
'#type' => 'checkbox',
'#default_value' => $whatsapp_value,
);
break;
}
return $element;
}
/**
* Implements hook_field_widget_error().
*
* hook_field_widget_error() lets us figure out what to do with errors
* we might have generated in hook_field_validate(). Generally, we'll just
* call form_error().
*
* @see bras_field_phone_field_validate()
* @see form_error()
*/
function bras_field_phone_field_widget_error($element, $error, $form, &$form_state) {
switch ($error['error']) {
case 'bras_field_phone_invalid':
form_error($element, $error['message']);
break;
}
}
function _bras_field_phone_ico_content($type) {
$path = drupal_get_path('module', 'bras_field_phone');
$img = '<img width="32px" height="32px" src="/' . $path . '/icons/ico_' . $type. '.png">';
$content = '<span class="icons-' .$type . '"> ' . $img .'</span>';
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment