Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @file
- * Custom functionality.
- */
- /**
- * Implements hook_menu().
- */
- function custom_menu() {
- $items = [];
- $items['homepage'] = [
- 'title' => '',
- 'page callback' => 'custom_blank_page',
- // No need to restrict access.
- 'access callback' => TRUE,
- ];
- $items['about-us'] = [
- 'title' => '',
- 'page callback' => 'custom_blank_page',
- // No need to restrict access.
- 'access callback' => TRUE,
- ];
- $items['contacts'] = [
- 'title' => '',
- 'page callback' => 'custom_blank_page',
- // No need to restrict access.
- 'access callback' => TRUE,
- ];
- $items['admin/content/content-settings'] = [
- 'title' => 'Content settings',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => ['custom_content_settings_form'],
- 'access arguments' => ['access administration menu'],
- ];
- return $items;
- }
- function custom_blank_page() {
- return '';
- }
- /**
- * Implements hook_entity_info_alter().
- */
- function custom_entity_info_alter(&$entity_info) {
- $entity_info['node']['view modes']['node_in_block'] = [
- 'label' => t('Node in block'),
- 'custom settings' => TRUE,
- ];
- $entity_info['node']['view modes']['node_in_block_offset'] = [
- 'label' => t('Node in block (with offset)'),
- 'custom settings' => TRUE,
- ];
- $entity_info['node']['view modes']['chess'] = [
- 'label' => t('Chess'),
- 'custom settings' => TRUE,
- ];
- $entity_info['node']['view modes']['about_us'] = [
- 'label' => t('About Us'),
- 'custom settings' => TRUE,
- ];
- $entity_info['taxonomy_term']['view modes']['services'] = [
- 'label' => t('Services'),
- 'custom settings' => TRUE,
- ];
- $entity_info['taxonomy_term']['view modes']['chess'] = $entity_info['node']['view modes']['chess'];
- }
- /**
- * Implements hook_field_widget_info().
- */
- function custom_field_widget_info() {
- return [
- 'text_email' => [
- 'label' => t('Simple E-mail'),
- 'field types' => ['text'],
- ],
- ];
- }
- /**
- * Implements hook_field_widget_form().
- */
- function custom_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
- $main_widget = [];
- switch ($instance['widget']['type']) {
- case 'text_email':
- $new_element = [
- '#type' => 'textfield',
- '#default_value' => isset($items[$delta]['value']) ?? NULL,
- '#size' => 20,
- '#maxlength' => $field['settings']['max_length'],
- '#attributes' => ['class' => array('text-full')],
- '#element_validate' => ['custom_element_validate_email'],
- ];
- break;
- }
- if (!empty($new_element)) {
- $main_widget = $element + $new_element;
- }
- $element['value'] = $main_widget;
- return $element;
- }
- /**
- * Implements hook_field_formatter_info().
- */
- function custom_field_formatter_info() {
- return [
- 'text_summary_and_body' => [
- 'label' => t('Summary and Body'),
- 'field types' => ['text_with_summary'],
- ],
- ];
- }
- /**
- * Implements hook_field_formatter_view().
- */
- function custom_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
- $element = array();
- switch ($display['type']) {
- case 'text_summary_and_body':
- foreach ($items as $delta => $item) {
- $output = '';
- foreach (['summary', 'value'] as $part) {
- $content = [
- '#theme' => 'html_tag',
- '#tag' => 'div',
- '#value' => _text_sanitize($instance, $langcode, $item, $part),
- '#attributes' => [
- 'class' => ["$part-wrapper"],
- ],
- ];
- $output .= drupal_render($content);
- }
- $element[$delta] = array('#markup' => $output);
- }
- break;
- }
- return $element;
- }
- /**
- * Implements hook_field_extra_fields().
- */
- function custom_field_extra_fields() {
- $extra['node']['article'] = [
- 'display' => [
- 'read_more' => [
- 'label' => t('Read more'),
- 'description' => t('Read more link'),
- 'weight' => 9,
- 'callback' => 'custom_read_more_link',
- ],
- 'publish_date' => [
- 'label' => t('Publish Date'),
- 'description' => t('Publish Date (d.m.Y)'),
- 'weight' => 10,
- ],
- 'view_counter' => [
- 'label' => t('Node view counter'),
- 'description' => t('Node view counter'),
- 'weight' => 11,
- ],
- ],
- ];
- return $extra;
- }
- function custom_read_more_link($entity) {
- $content = l(t('Read more'), 'node/' . $entity->nid);
- $content .= custom_get_chevron();
- return '<div class="read-more-default">' . $content . '</div>';
- }
- function custom_get_material_icon($name, $tag = 'div') {
- $icon = [
- '#theme' => 'html_tag',
- '#tag' => $tag,
- '#value' => '',
- '#attributes' => ['class' => ['zmdi', "zmdi-$name"]],
- ];
- return drupal_render($icon);
- }
- function custom_get_chevron() {
- return custom_get_material_icon('chevron-right');
- }
- /**
- * Implements hook_form_alter().
- */
- function custom_form_alter(&$form, &$form_state, $form_id) {
- switch ($form_id) {
- case 'lang_dropdown_form':
- if (!empty($form['lang_dropdown_select']['#options'])) {
- $options =& $form['lang_dropdown_select']['#options'];
- foreach ($options as &$option) {
- $option = drupal_substr($option, 0, 2);
- }
- }
- break;
- case 'views_exposed_form':
- switch ($form['#id']) {
- case 'views-exposed-form-content-page-search':
- $form['title']['#size'] = 10;
- $form['submit']['#attributes'] = [
- 'class' => ['zmdi', 'zmdi-search', 'search-btn'],
- ];
- break;
- }
- break;
- case 'feedback_node_form':
- if (!empty($_SESSION['messages']['status'])) {
- $messages =& $_SESSION['messages']['status'];
- if (count($messages) > 1) {
- array_pop($messages);
- }
- }
- foreach(['title', 'phone', 'email', 'message'] as $field_name) {
- if ('title' === $field_name) {
- $field_element =& $form[$field_name . '_field'][LANGUAGE_NONE][0]['value'];
- }
- else {
- $field_element =& $form["field_$field_name"][LANGUAGE_NONE][0]['value'];
- }
- $placeholder = $field_element['#title'];
- if (!empty($placeholder)) {
- $placeholder = drupal_strtolower($placeholder);
- $placeholder = explode(' ', $placeholder);
- array_shift($placeholder);
- $placeholder = t('Enter your @placeholder',['@placeholder' => implode(' ', $placeholder)]);
- $field_element['#attributes']['placeholder'] = $placeholder;
- }
- }
- $form['actions']['submit']['#value'] = t('Send');
- $form['actions']['submit']['#attributes']['class'][] = 'black-button';
- $form['#submit'][] = 'custom_feedback_node_form_submit';
- break;
- }
- }
- function custom_feedback_node_form_submit(&$form, $form_state) {
- drupal_set_message(t('!Thnx Your message has been sent.', [
- '!Thnx' => '<b>' . t('Thank you.') . '</b></br>',
- ]));
- }
- /**
- * Implements hook_block_info().
- */
- function custom_block_info() {
- $blocks = [];
- $blocks['custom_contact_info'] = [
- 'info' => t('Contacts'),
- 'cache' => DRUPAL_NO_CACHE
- ];
- $blocks['custom_contacts'] = [
- 'info' => t('Contacts: Address & e-mail'),
- 'cache' => DRUPAL_NO_CACHE
- ];
- $blocks['custom_phone'] = [
- 'info' => t('Contacts: Phone'),
- 'cache' => DRUPAL_NO_CACHE
- ];
- $blocks['custom_socials'] = [
- 'info' => t('Contacts: Socials'),
- 'cache' => DRUPAL_NO_CACHE
- ];
- $blocks['custom_scroll_status'] = [
- 'info' => t('Scroll Status'),
- 'cache' => DRUPAL_NO_CACHE
- ];
- return $blocks;
- }
- /**
- * Implements hook_block_view().
- */
- function custom_block_view($delta = '') {
- static $contacts;
- static $socials;
- $block = [];
- if (is_null($contacts)) {
- $settings = variable_get('custom_site_settings', []);
- $contacts = array_filter($settings, 'is_scalar');
- $socials = array_diff_key($settings, $contacts);
- $socials = array_shift($socials);
- }
- switch ($delta) {
- case 'custom_scroll_status':
- $block = [
- 'content' => '<div class="scroll-status"></div>',
- ];
- break;
- case 'custom_contact_info':
- $content = [];
- foreach ($contacts as $item => $value) {
- $value = nl2br($value);
- $value = [
- '#theme' => 'html_tag',
- '#tag' => 'div',
- '#value' => '<div class="content">' . $value . '</div>',
- '#attributes' => [
- 'class' => ['contact-item-wrapper', "wrapper-$item"],
- ],
- ];
- $content[] = drupal_render($value);
- }
- $content = implode('', $content);
- // @todo: temporary hide socials, uncommment after account creation.
- // $content .= custom_get_social_icons($socials);
- $block = [
- 'title' => t('Contacts'),
- 'content' => $content,
- ];
- break;
- case 'custom_contacts':
- case 'custom_phone':
- $content = [];
- $fields = [
- 'custom_contacts' => ['address', 'e_mail'],
- 'custom_phone' => ['phone'],
- ];
- foreach ($fields[$delta] as $item) {
- if (!empty($contacts[$item])) {
- $value = $contacts[$item];
- if ('e_mail' === $item) {
- $value = '<div class="zmdi zmdi-email"></div>' . $value;
- }
- $item = [
- '#theme' => 'html_tag',
- '#tag' => 'div',
- '#value' => $value,
- '#attributes' => [
- 'class' => ['contact-wrapper', "wrapper-$item"],
- ],
- ];
- $content[] = drupal_render($item);
- }
- }
- $block = ['content' => implode('', $content)];
- break;
- case 'custom_socials':
- // @todo: temporary hide socials, uncommment after account creation.
- // $block = ['content' => custom_get_social_icons($socials)];
- $block = ['content' => ''];
- break;
- }
- return $block;
- }
- /**
- * Implements hook_block_view_alter().
- */
- function custom_block_view_alter(&$data, $block) {
- switch ($block->module) {
- case 'formblock':
- switch ($block->delta) {
- case 'feedback':
- $data['content']['secondary-title'] = [
- '#prefix' => '<div class="secondary-title">',
- '#markup' => t('Still have questions ?'),
- '#suffix' => '</div>',
- '#weight' => -1,
- ];
- break;
- }
- break;
- case 'system':
- case 'multiblock':
- switch ($block->delta) {
- case 'powered-by':
- case 1:
- case 4:
- $data["content"] = t("© !name 2019", [
- '!name' => variable_get('site_name', 'Devpoint'),
- ]);
- break;
- }
- break;
- }
- }
- /**
- * Implements hook_field_group_pre_render_alter().
- */
- function custom_field_group_pre_render_alter(&$element, $group, &$form) {
- switch ($group->mode) {
- case 'chess':
- static $counter = null;
- static $services = -1;
- $groups = [
- 'group_left_group' => 'push',
- 'group_right_group' => 'pull',
- 'group_middle_group' => 'pull',
- ];
- if ('taxonomy_term' !== $group->entity_type) {
- unset($groups['group_middle_group']);
- }
- elseif ('services' === $group->bundle) {
- if ($services < 0) {
- $results = views_get_view_result('taxonomy', 'page_1');
- $services = count($results) - 1;
- }
- }
- $steps = count($groups);
- if (is_null($counter)) {
- $counter = $steps;
- }
- if (array_key_exists($group->group_name, $groups)) {
- $is_even = $counter < 0;
- if ($is_even) {
- $chess_classes = [];
- $operation = $groups[$group->group_name];
- foreach (explode(' ', $group->classes) as $class) {
- $parts = explode('-', $class);
- $media = array_intersect($parts, ['lg', 'md', 'sm']);
- if (!empty($media)) {
- $media = reset($media);
- $size = 12 - end($parts);
- $new_class = [reset($parts), $media, $operation, $size];
- $chess_classes[] = implode('-', $new_class);
- }
- elseif (!$services && 'group_left_group' === $group->group_name) {
- $xs_class = array_intersect($parts, ['xs']);
- if (!empty($xs_class)) {
- $element['#prefix'] = str_replace($class, 'col-xs-12', $element['#prefix']);
- --$services;
- }
- }
- }
- $element['#prefix'] = custom_insert_classes($element['#prefix'], $chess_classes);
- }
- $step = $counter / abs($counter) * (-1);
- $counter += $step;
- if (!$counter) {
- $counter = $steps * $step;
- --$services;
- }
- }
- break;
- }
- }
- /**
- * Implements hook_node_view().
- */
- function custom_node_view($node, $view_mode, $langcode) {
- switch ($view_mode) {
- case 'chess':
- case 'teaser':
- $node->title = NULL;
- break;
- }
- switch ($node->type) {
- case 'article':
- case 'page':
- switch ($view_mode) {
- case 'chess':
- static $counter = 1;
- $styles = [
- 'article' => 'mediator_reverse',
- 'page' => 'our_work_left',
- ];
- if ($counter++ % 2) {
- $node->content['field_image'][0]['#image_style'] = $styles[$node->type];
- }
- break;
- }
- break;
- }
- $fields = ['read_more', 'publish_date', 'view_counter'];
- $extra_fields = field_extra_fields_get_display('node', $node->type, $view_mode);
- foreach ($fields as $field) {
- if (!empty($extra_fields[$field]['visible'])) {
- switch ($field) {
- case 'view_counter':
- if (module_exists('nodeviewcount')) {
- // Yes, we really need to suppress errors, because of contrib module.
- @$amount = nodeviewcountcount_count_node_views($node);
- while (!is_scalar($amount)) {
- $amount = (array) $amount;
- $amount = reset($amount);
- }
- $node->content[$field]['#prefix'] = '<div class="pseudo-field node-view-counter">';
- $node->content[$field]['#markup'] = $amount;
- $node->content[$field]['#suffix'] = '</div>';
- }
- break;
- case 'publish_date':
- $date = format_date($node->created, 'custom', 'd.m.Y');
- $node->content[$field]['#prefix'] = '<div class="pseudo-field publish-date">';
- $node->content[$field]['#markup'] = $date;
- $node->content[$field]['#suffix'] = '</div>';
- break;
- case 'read_more':
- switch ($view_mode) {
- case 'chess':
- $node->content[$field]['#markup'] = custom_read_more_link($node);
- break;
- default:
- if (!empty($node->nodeblock['node_link'])) {
- $node->content[$field] = ['#markup' => ''];
- if (in_array($view_mode, ['node_in_block', 'node_in_block_offset'])) {
- $node->content[$field]['#markup'] .= custom_get_chevron();
- $node->content[$field]['#prefix'] = '<div class="black-button">';
- $node->content[$field]['#suffix'] = '</div>';
- }
- /** @noinspection SuspiciousAssignmentsInspection */
- $node->content[$field]['#markup'] .= l(t('read more'), 'about-us');
- }
- break;
- }
- break;
- }
- }
- }
- }
- /**
- * Implements hook_taxonomy_term_view().
- */
- function custom_taxonomy_term_view($term, $view_mode, $langcode) {
- switch ($term->vocabulary_machine_name) {
- case 'services':
- switch ($view_mode) {
- case 'chess':
- $term->name = '';
- if (!empty($term->content['field_icons'][0])) {
- unset($term->content['field_icons'][0]);
- }
- break;
- case 'services':
- $term->name = '';
- break;
- }
- break;
- }
- }
- /**
- * Implements hook_preprocess_HOOK().
- */
- function custom_preprocess_page(&$variables) {
- $variables['secondary_search'] = module_invoke( 'search', 'block_view', 'form');
- $secondary_lang = module_invoke( 'lang_dropdown', 'block_view', 'language_content');
- $secondary_lang['subject'] = '';
- $variables['secondary_lang'] = $secondary_lang;
- if (!empty($variables['node'])) {
- $node =& $variables['node'];
- switch ($node->type) {
- case 'article':
- $variables['title'] = '';
- break;
- }
- }
- }
- /**
- * Implements hook_preprocess_HOOK().
- */
- function custom_preprocess_node(&$variables) {
- $variables['classes_array'][] = 'vm-' . $variables['view_mode'];
- }
- /**
- * Implements hook_preprocess_HOOK().
- */
- function custom_preprocess_taxonomy_term(&$variables) {
- $variables['classes_array'][] = 'vm-' . $variables['view_mode'];
- }
- /**
- * Implements hook_preprocess_HOOK().
- */
- function custom_preprocess_views_view_unformatted(&$variables) {
- switch ($variables['view']->name) {
- case 'dynamic_content':
- switch ($variables['view']->current_display) {
- case 'block_related_articles':
- if (module_exists('slick')) {
- $settings = ['optionset' => 'related_articles'];
- foreach ($variables['rows'] as $key => $row) {
- $variables['rows'][$key] = [
- 'slide' => $row,
- ];
- }
- $variables['rows'] = slick_build($variables['rows'], [], $settings);
- $variables['rows'] = [render($variables['rows'])];
- }
- break;
- }
- break;
- case 'taxonomy':
- switch ($variables['view']->current_display) {
- case 'block_services_images':
- if (!empty($variables['classes_array'][0])) {
- $variables['classes_array'][0] .= ' visible default';
- }
- break;
- }
- break;
- }
- }
- function custom_content_settings_form() {
- $form = [];
- $settings = variable_get('custom_site_settings', []);
- $form['custom_site_settings'] = array(
- '#type' => 'fieldset',
- '#title' => t('Global site settings'),
- '#collapsible' => FALSE,
- '#collapsed' => FALSE,
- '#tree' => TRUE,
- );
- foreach (custom_get_site_settings_fields() as $index => $title) {
- switch ($index) {
- case 'message':
- case 'address':
- $field_type = 'textarea';
- break;
- default:
- $field_type = 'textfield';
- break;
- }
- $form['custom_site_settings'][$index] = [
- '#type' => $field_type,
- '#title' => $title,
- '#required' => TRUE,
- '#default_value' => $settings[$index] ?? '',
- ];
- }
- $form['custom_site_settings']['social_settings'] = [
- '#type' => 'fieldset',
- '#title' => t('Social networks'),
- '#collapsible' => TRUE,
- '#collapsed' => FALSE,
- '#tree' => TRUE,
- ];
- foreach (custom_get_socials_settings() as $index => $title) {
- $form['custom_site_settings']['social_settings'][$index] = [
- '#type' => 'textfield',
- '#title' => $title,
- '#required' => FALSE,
- '#default_value' => $settings['social_settings'][$index] ?? '',
- ];
- }
- return system_settings_form($form);
- }
- function custom_get_site_settings_fields() {
- $fields = [
- 'message' => t('Contacts message'),
- 'address' => t('Address'),
- 'e_mail' => t('E-mail'),
- 'phone' => t('Phone'),
- ];
- return $fields;
- }
- function custom_get_socials_settings() {
- return [
- 'facebook' => t('Facebook'),
- 'google-plus' => t('Google Plus'),
- 'twitter' => t('Twitter'),
- ];
- }
- /**
- * Implements hook_preprocess_HOOK().
- */
- function custom_preprocess_views_slideshow_pager_field_item(&$variables) {
- if (!empty($variables['item'])) {
- $old_value = strip_tags($variables['item']);
- $new_value = sprintf(".%'.02d", $old_value);
- $variables['item'] = str_replace(">$old_value<", ">$new_value<", $variables['item']);
- }
- }
- /**
- * Implements hook_preprocess_HOOK().
- */
- function custom_process_views_infinite_scroll_pager(&$variables) {
- // On the last page button will be empty and we don't need class there.
- if (!empty(drupal_render($variables['button']))) {
- $variables['automatic_scroll_class'] .= 'black-button';
- }
- }
- /**
- * Implements hook_preprocess_HOOK().
- */
- function custom_preprocess_picture(&$variables) {
- $variables['uri'] = custom_swap_to_webp($variables['uri']);
- }
- /**
- * Implements hook_preprocess_HOOK().
- */
- function custom_preprocess_image(&$variables) {
- $variables['path'] = custom_swap_to_webp($variables['path']);
- }
- /**
- * Implements hook_preprocess_HOOK().
- */
- function custom_preprocess_lazy_image(&$variables) {
- $path = preg_replace('"\.(bmp|gif|png|jpg)"', '.WebP', $variables['path']);
- $image_style_path = image_style_path($variables['style_name'], $path);
- if (file_exists(drupal_realpath($image_style_path))) {
- $variables['path'] = $path;
- }
- }
- function custom_swap_to_webp($uri) {
- $filename = preg_replace('"\.(bmp|gif|png|jpg)"', '.webp', $uri);
- $scheme = file_uri_scheme($filename);
- switch ($scheme) {
- case 'http':
- case 'https':
- $headers = get_headers($filename);
- if(stripos($headers[0],"200 OK")) {
- $uri = $filename;
- }
- break;
- case 'public':
- $file_path = drupal_realpath($filename);
- if (file_exists($file_path)) {
- $uri = $filename;
- }
- break;
- }
- return $uri;
- }
- function custom_get_social_icons($socials) {
- foreach ($socials as $name => $url) {
- if (!empty($url)) {
- $item = [
- '#theme' => 'html_tag',
- '#tag' => 'div',
- '#value' => l('', $url, [
- 'html' => TRUE,
- 'attributes' => [
- 'class' => ['zmdi', "zmdi-$name"],
- ],
- ]),
- '#attributes' => [
- 'class' => ['zmdi-social-wrapper'],
- ],
- ];
- $content[] = drupal_render($item);
- }
- }
- return '<div class="main-social-wrapper">' . implode('', $content) . '</div>';
- }
- function custom_insert_classes($html, $classes) {
- $matches = [];
- $pattern = '/class="(.+?)"/i';
- preg_match($pattern, $html, $matches);
- if (!empty($matches[1])) {
- $old_classes = $matches[1];
- $new_classes = explode(' ', $old_classes);
- $new_classes = array_merge($new_classes, $classes);
- $new_classes = implode(' ', $new_classes);
- $html = str_replace($old_classes, $new_classes, $html);
- }
- return $html;
- }
- /**
- * Implements hook_contextual_links_view_alter().
- */
- function custom_contextual_links_view_alter(&$element, $items) {
- if (!empty($element['#element']['#views_contextual_links_info']['views_ui'])) {
- $views_ui =& $element['#element']['#views_contextual_links_info']['views_ui'];
- switch ($views_ui['view_name']) {
- case 'dynamic_content':
- switch ($views_ui['view_display_id']) {
- case 'block_related_articles':
- $element['#links']['slick-ui-edit'] = [
- 'title' => t('Edit slick carousel'),
- 'href' => 'admin/config/media/slick/list/related_articles/edit',
- ];
- break;
- }
- break;
- }
- }
- }
- /**
- * Implements hook_image_effect_info().
- */
- function custom_image_effect_info() {
- $effects = array(
- 'custom_image_replace_gradient' => [
- 'label' => t('Replace Gradient'),
- 'help' => t('Partial replace of an image by effect'),
- 'effect callback' => 'custom_image_replace_gradient_effect',
- 'form callback' => 'custom_image_default_options_form',
- ],
- 'custom_image_replace_greyscale' => [
- 'label' => t('Replace Greyscale'),
- 'help' => t('Partial replace of an image by effect'),
- 'effect callback' => 'custom_image_replace_greyscale_effect',
- 'form callback' => 'custom_image_default_options_form',
- ],
- 'custom_image_phone_watermark' => [
- 'label' => t('Replace Phone Watermark'),
- 'help' => t('Partial replace of an image by effect'),
- 'effect callback' => 'custom_image_phone_watermark_effect',
- 'form callback' => 'custom_image_default_options_form',
- ],
- );
- return $effects;
- }
- function custom_image_default_options_form($data) {
- $fields = [
- 'offset_x' => t('Offset X'),
- 'offset_y' => t('Offset Y'),
- 'height' => t('Height'),
- 'width' => t('Width'),
- ];
- foreach ($fields as $name => $title) {
- $form[$name] = [
- '#type' => 'textfield',
- '#title' => $title,
- '#default_value' => $data[$name] ?? '',
- '#field_suffix' => ' ' . t('%'),
- '#required' => TRUE,
- '#size' => 10,
- '#element_validate' => ['element_validate_integer'],
- '#allow_negative' => FALSE,
- ];
- }
- return $form;
- }
- function custom_image_get_file($name) {
- $path = [
- drupal_get_path('module', 'custom'),
- 'images',
- $name,
- ];
- return implode('/', $path);
- }
- function custom_image_phone_watermark_effect($source, $data) {
- $image = $source->resource;
- $im_width = imagesx($image);
- $im_height = imagesy($image);
- $blank = imagecreatetruecolor($im_width + abs($data['width']), $im_height + abs($data['height']));
- $b_width = imagesx($blank) * $data['offset_x'] / 100;
- $b_height = imagesy($blank) * $data['offset_y'] / 100;
- imagesavealpha($blank, true);
- $transparent = imagecolorallocatealpha($blank, 0, 0, 0, 127);
- imagefill($blank, 0, 0, $transparent);
- imagelayereffect($blank, IMG_EFFECT_REPLACE);
- $dst_x = $data['width'] < 0 ? abs($data['width']) : 0;
- imagecopy($blank, $image, $dst_x, 0, 0, 0, $im_width, $im_height);
- $path = custom_image_get_file('phone-watermark.png');
- $phone = imagecreatefrompng($path);
- $ph_width = imagesx($phone);
- $ph_height = imagesy($phone);
- if ($data['width'] < 0) {
- $dst_x = 0;
- }
- else {
- $dst_x = $b_width - $ph_width;
- if ($dst_x < 0) {
- $dst_x = 0;
- }
- }
- imagelayereffect($blank, IMG_EFFECT_ALPHABLEND);
- imagecopy($blank, $phone, $dst_x, $b_height - $ph_height, 0, 0, $ph_width, $ph_height);
- $source->resource = $blank;
- return TRUE;
- }
- function custom_image_replace_gradient_effect($source, $data) {
- $image = $source->resource;
- $im_width = imagesx($image);
- $im_height = imagesy($image);
- foreach ($data as &$item) {
- $item /= 100;
- }
- $data = [
- 'x' => $im_width * $data['offset_x'],
- 'y' => $im_height * $data['offset_y'],
- 'width' => $im_width * $data['width'],
- 'height' => $im_height * $data['height'],
- ];
- $part = imagecrop($image, $data);
- $path = drupal_get_path('module', 'custom');
- $path .= '/images/gradient.png';
- $overlayImage = imagecreatefrompng($path);
- $overlayImage = imagescale($overlayImage, $data['width'], $data['height']);
- imagelayereffect($overlayImage, IMG_EFFECT_OVERLAY);
- imagecopy($overlayImage, $part, 0, 0, 0, 0, $data['width'], $data['height']);
- imagelayereffect($image, IMG_EFFECT_REPLACE);
- imagecopy($image, $overlayImage, $data['x'], $data['y'], 0, 0, $data['width'], $data['height']);
- imageDestroy($part);
- imageDestroy($overlayImage);
- return TRUE;
- }
- function custom_image_replace_greyscale_effect($source, $data) {
- $image = $source->resource;
- $im_width = imagesx($image);
- $im_height = imagesy($image);
- foreach ($data as &$item) {
- $item /= 100;
- }
- $data = [
- 'x' => $im_width * $data['offset_x'],
- 'y' => $im_height * $data['offset_y'],
- 'width' => $im_width * $data['width'],
- 'height' => $im_height * $data['height'],
- ];
- $part = imagecrop($image, $data);
- imagefilter($part, IMG_FILTER_GRAYSCALE);
- imagelayereffect($image, IMG_EFFECT_REPLACE);
- imagecopy($image, $part, $data['x'], $data['y'], 0, 0, $data['width'], $data['height']);
- imageDestroy($part);
- return TRUE;
- }
- /**
- * Implements hook_field_formatter_settings_form_alter().
- */
- function custom_field_formatter_settings_form_alter(array &$settings_form, array $context) {
- // $settings_form['single_image_delta']['#element_validate'] = ['element_validate_integer'];
- }
- function custom_element_validate_email($element, &$form_state) {
- $email = trim($element['#value']);
- if (!empty($email) && !valid_email_address($email)) {
- form_error($element, t('%name is not a valid email address', [
- '%name' => $element['#value'],
- ]));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement