You are here

LinkImageField.php in Link Image Field 8

File

lib/Drupal/linkimagefield/Plugin/Field/FieldType/LinkImageField.php
View source
<?php

namespace Drupal\linkimagefield\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\image\Plugin\Field\FieldType\ImageItem;

/**
 * Plugin implementation of the 'LinkImageField' field type.
 *
 * @FieldType(
 *   id = "linkimagefield",
 *   label = @Translation("Link Image"),
 *   description = @Translation("An edit widget for image files that display as a link, including a preview of the image."),
 *   default_widget = "linkimagefield_widget",
 *   default_formatter = "linkimagefield"
 * )
 */
class LinkImageField extends ImageItem {

  /**
   * {@inheritdoc}
   */
  public static function defaultInstanceSettings() {
    return array(
      'longdesc_field' => '0',
      'url_settings' => array(
        'url' => '',
        'rel_field' => '0',
        'class_field' => '0',
        'custom_target' => '0',
        'target' => '_self',
      ),
    ) + parent::defaultInstanceSettings();
  }

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldDefinitionInterface $field_definition) {
    return array(
      'columns' => array(
        'target_id' => array(
          'description' => 'The ID of the file entity.',
          'type' => 'int',
          'not null' => FALSE,
          'unsigned' => TRUE,
        ),
        'alt' => array(
          'description' => "Alternative image text, for the image's 'alt' attribute.",
          'type' => 'varchar',
          'length' => 128,
          'not null' => FALSE,
        ),
        'longdesc' => array(
          'description' => "A link to a page with a long description of the image, for the image's 'londesc' attribute.",
          'type' => 'varchar',
          'length' => 128,
          'not null' => FALSE,
        ),
        'title' => array(
          'description' => "Image title text, for the image's 'title' attribute.",
          'type' => 'varchar',
          'length' => 128,
          'not null' => FALSE,
        ),
        'url' => array(
          'description' => "The URL used for the image link.",
          'type' => 'varchar',
          'length' => 128,
          'not null' => FALSE,
        ),
        'target' => array(
          'description' => "The target type of the image link.",
          'type' => 'varchar',
          'length' => 128,
          'not null' => FALSE,
        ),
        'class' => array(
          'description' => "For adding a class attribute to the anchor.",
          'type' => 'varchar',
          'length' => 128,
          'not null' => FALSE,
        ),
        'rel' => array(
          'description' => "A relation tag for bots or lightboxes.",
          'type' => 'varchar',
          'length' => 128,
          '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(
        'target_id' => array(
          'target_id',
        ),
      ),
      'foreign keys' => array(
        'target_id' => array(
          'table' => 'file_managed',
          'columns' => array(
            'target_id' => 'fid',
          ),
        ),
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldDefinitionInterface $field_definition) {
    $properties = parent::propertyDefinitions($field_definition);
    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public function instanceSettingsForm(array $form, array &$form_state) {
    $element = parent::instanceSettingsForm($form, $form_state);
    $settings = $this
      ->getSettings();
    $element['url_settings'] = array(
      '#type' => 'details',
      '#title' => t('URL Link settings'),
      '#open' => TRUE,
      '#weight' => 0,
    );
    $element['url_settings']['url'] = array(
      '#type' => 'textfield',
      '#title' => t('Default URL'),
      '#description' => t('Provide a well-formed URL.  This will be the default url linked to by provided images.'),
      '#default_value' => $settings['url_settings']['url'],
      '#maxlength' => '255',
    );
    $element['url_settings']['rel_field'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable link <em>rel</em> field'),
      '#default_value' => $settings['url_settings']['rel_field'],
      '#description' => t('Allow rel attributes to be added to links'),
    );
    $element['url_settings']['class_field'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable link <em>class</em> field'),
      '#default_value' => $settings['url_settings']['class_field'],
      '#description' => t('Allow classes to be added to links'),
    );
    $target_options = _linkimagefield_widget_url_target_options();
    $element['url_settings']['custom_target'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable custom target'),
      '#default_value' => $settings['url_settings']['custom_target'],
      '#description' => t('Enable user to provide alternate target frame for link.'),
    );
    $element['url_settings']['target'] = array(
      '#type' => 'select',
      '#title' => t('Default Target'),
      '#description' => t('Select a default target type.'),
      '#default_value' => $settings['url_settings']['target'],
      '#options' => $target_options,
      '#maxlength' => '255',
    );
    $element['longdesc_field'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable <em>Longdesc</em> field'),
      '#default_value' => $settings['longdesc_field'],
      '#description' => t('Allow the longdesc attribute to be added to images'),
      '#weight' => 10,
    );
    return $element;
  }

}

Classes

Namesort descending Description
LinkImageField Plugin implementation of the 'LinkImageField' field type.