cforge-2.0.x-dev/src/Plugin/Field/FieldType/ContactMeItem.php

src/Plugin/Field/FieldType/ContactMeItem.php
<?php

namespace Drupal\cforge\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'worth' field type.
 *
 * @FieldType(
 *   id = "cf_contactme",
 *   label = @Translation("Contact me"),
 *   description = @Translation("Phone numbers and social media channels"),
 *   default_widget = "cf_contactme",
 *   default_formatter = "cf_contactme"
 * )
 */
class ContactMeItem extends FieldItemBase {

  const CHANNELS = [
    'altmail' => [
      'title' => 'Email (alternative)',
      'regex' => '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$',
      'example' => 'john.doe@example.com',
      'prefix' => 'mailto:'
    ],
    'mob' => [
      'title' => 'Main phone (mobile)',
      'example' => '99 999 9999',
      'prefix' => 'tel:'
    ],
    'tel' => [
      'title' => 'Second phone',
      'example' => '99 999 9999',
      'prefix' => 'tel:'
    ],
    'web' => [
      'title' => 'Web site',
      'prefix' => 'https://',
      'regex' => '^[A-Za-z0-9_-]+\.[A-Za-z0-9_.-\/]+$',
      'example' => 'example.com'
    ],
    'whatsapp' => [
      'title' => 'Whatsapp number',
      'prefix' => 'https://wa.me/',
      'phoneid' => TRUE, // not used ATM
    ],
    'telegram' => [
      'title' => 'Telegram handle',
      'prefix' => 'https://telegram.me/',
      'example' => 'johndoe01',
    ],
    'twitter' => [
      'title' => 'Twitter handle',
      'prefix' => 'https://twitter.com/@',
      'regex' => '^[A-Za-z0-9_]{4,50}$',
      'example' => 'JohnDoe_01'
    ],
    'instagram' => [
      'title' => 'Instagram handle',
      'prefix' => 'https://instagram.com/',
      'regex' => '^[A-Za-z0-9_.]{4,30}$',
      'example' => 'john.doe_01'
    ],
    'linkedin' => [
      'title' => 'Linkedin handle',
      'prefix' => 'https://linkedin.com/in/',
      'regex' => '^[A-Za-z0-9_-]{5,}$',
      'example' => 'john-doe_01'
    ],
    'facebook' => [
      'title' => 'Facebook page',
      'prefix' => 'https://facebook.com/',
      'regex' => '^[a-zA-Z0-9._]{5,}$',
      'example' => 'john.doe_01'
    ],
    'skype' => [
      'title' => 'Skype handle',
      'prefix' => 'skype:',
      'regex' => '^[a-zA-Z][a-zA-Z0-9\.,\-_]{5,31}$',
      'example' => 'john-doe_01'
    ],
    // can't add this until I know how to update the db.
//    'discord' => [
//      'title' => 'Discord handle',
//      'prefix' => 'skype:',
//      'regex' => '^[a-z]+:[0-9]{,5}',
//      'example' => 'johndoe#1234'
//    ]
  ];

  /**
   * {@inheritDoc}
   */
  public static function propertydefinitions(FieldStorageDefinitionInterface $field_definition) {
    foreach (static::CHANNELS as $id => $info) {
      $label = new TranslatableMarkup($info['title']);
      $properties[$id] = DataDefinition::create('string')->setLabel($label);
    }
    return $properties;
  }

  /**
   * {@inheritDoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    foreach (static::CHANNELS as $id => $info) {
      $columns[$id] = [
        'description' => $info['title'],
        'type' => 'varchar',
        'length' => '32',
      ];
    }
    $columns['web']['length'] = 64;
    $columns['facebook']['length'] = 64;

    return ['columns'=> $columns];
  }

  /**
   * {@inheritDoc}
   */
  public function isEmpty() {
    return empty(trim($this->tel)) and empty(trim($this->mob));
  }

  /**
   * {@inheritDoc}
   */
  public static function defaultFieldSettings() {
    return ['channels' => ['mob', 'facebook', 'whatsapp']];
  }

  /**
   * {@inheritDoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    foreach (static::CHANNELS as $id => $info) {
      $sample[$id] = $info['example'];
    }
    return $sample;
  }

  /**
   * {@inheritDoc}
   */
  public static function mainPropertyName() {
    return 'mob';
  }

  /**
   * {@inheritDoc}
   */
  public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
    foreach (ContactMeItem::CHANNELS as $channel => $info) {
      $options[$channel] = new TranslatableMarkup($info['title']);
    }
    $form['channels'] = [
      '#title' => $this->t('Enabled channels'),
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => $this->getSetting('channels')
    ];
    $form['channels']['mob']['#disabled'] = TRUE;
    $form['channels']['mob']['#value'] = TRUE;
    return $form;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc