K
K
Kirill Parasotchenko2016-01-08 15:57:29
PHP
Kirill Parasotchenko, 2016-01-08 15:57:29

New drupal field type with Image widget. The picture is not displayed. How to fix?

Hello!
I'm trying to create a new field type for an Article (drupal 7). The new field uses the existing Image widget.
The image is uploaded to the server. But when viewing the article it is not displayed. Maybe someone knows how to fix it?
I have apache 2.4/php 7/mysql 5
Here is the code to create the field:

<?php

function my_watermark_field_info()
{
    return array(
        'my_watermark_image_with_watermark' => array(
            'label' => t('Image with watermark'),
            'description' => t('Demonstrates a field composed of an RGB color.'),
            'settings' => array(
                'uri_scheme' => variable_get('file_default_scheme', 'public'),
                'default_image' => 0,
            ),
            'instance_settings' => array(
                'file_extensions' => 'png gif jpg jpeg',
                'file_directory' => '',
                'max_filesize' => '',
                'alt_field' => 0,
                'title_field' => 0,
                'max_resolution' => '',
                'min_resolution' => '',
                'default_image' => 0,
            ),
            'default_widget' => 'image_image',
            'default_formatter' => 'image',
        )
    );
}

/**
 * Implements hook_field_widget_info_alter().
 */
function my_watermark_field_widget_info_alter(&$info)
{
    $info['image_image']['field types'][] = 'my_watermark_image_with_watermark';
}

/**
 * Implements hook_field_formatter_info_alter().
 */
function my_watermark_field_formatter_info_alter(&$info)
{
    $info['image']['field types'][] = 'my_watermark_image_with_watermark';
}


/**
 * Implements hook_field_load().
 */
function my_watermark_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age)
{
    file_field_load($entity_type, $entities, $field, $instances, $langcode, $items, $age);
    
}

/**
 * Implements hook_field_prepare_view().
 */
function my_watermark_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items)
{
    // If there are no files specified at all, use the default.
    foreach ($entities as $id => $entity)
    {
        if (empty($items[$id]))
        {
            $fid = 0;
            // Use the default for the instance if one is available.
            if (!empty($instances[$id]['settings']['default_image']))
            {
                $fid = $instances[$id]['settings']['default_image'];
            }
            // Otherwise, use the default for the field.
            elseif (!empty($field['settings']['default_image']))
            {
                $fid = $field['settings']['default_image'];
            }

            // Add the default image if one is found.
            if ($fid && ($file = file_load($fid)))
            {
                $items[$id][0] = (array) $file + array(
                    'is_default' => TRUE,
                    'alt' => '',
                    'title' => '',
                );
            }
        }
    }
}

/**
 * Implements hook_field_presave().
 */
function my_watermark_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items)
{
    file_field_presave($entity_type, $entity, $field, $instance, $langcode, $items);

    // Determine the dimensions if necessary.
    foreach ($items as &$item)
    {
        if (!isset($item['width']) || !isset($item['height']))
        {
            $info = image_get_info(file_load($item['fid'])->uri);

            if (is_array($info))
            {
                $item['width'] = $info['width'];
                $item['height'] = $info['height'];
            }
        }
    }
}

/**
 * Implements hook_field_insert().
 */
function my_watermark_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items)
{
    file_field_insert($entity_type, $entity, $field, $instance, $langcode, $items);
}

/**
 * Implements hook_field_update().
 */
function my_watermark_field_update($entity_type, $entity, $field, $instance, $langcode, &$items)
{
    file_field_update($entity_type, $entity, $field, $instance, $langcode, $items);
}

/**
 * Implements hook_field_delete().
 */
function my_watermark_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items)
{
    file_field_delete($entity_type, $entity, $field, $instance, $langcode, $items);
}

/**
 * Implements hook_field_delete_revision().
 */
function my_watermark_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items)
{
    file_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, $items);
}

/**
 * Implements hook_field_is_empty().
 */
function my_watermark_field_is_empty($item, $field)
{
    return file_field_is_empty($item, $field);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Parasotchenko, 2016-01-08
@Taiyonoryoshiy

in general, it was necessary to correctly write hook_field_schema in the install file
, here is the code

function my_watermark_field_schema($field) {
  return array(
    'columns' => array(
      'fid' => array(
        'description' => 'The {file_managed}.fid being referenced in this field.',
        'type' => 'int',
        'not null' => FALSE,
        'unsigned' => TRUE,
      ),
      'alt' => array(
        'description' => "Alternative image text, for the image's 'alt' attribute.",
        'type' => 'varchar',
        'length' => 512,
        'not null' => FALSE,
      ),
      'title' => array(
        'description' => "Image title text, for the image's 'title' attribute.",
        'type' => 'varchar',
        'length' => 1024,
        'not null' => FALSE,
      ),
      'width' => array(
        'description' => 'The width of the image in pixels.',
        'type' => 'int',
        'unsigned' => TRUE,
      ),
      'height' => array(
        'description' => 'The height of the image in pixels.',
        'type' => 'int',
        'unsigned' => TRUE,
      ),
    ),
    'indexes' => array(
      'fid' => array('fid'),
    ),
    'foreign keys' => array(
      'fid' => array(
        'table' => 'file_managed',
        'columns' => array('fid' => 'fid'),
      ),
    ),
  );
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question