1

I created a custom field because i wanted to store a multiple field with:

  • social type (Facebook, Twitter, etc..)
  • social ID

Everything shows fine, but when I save some data, nothing is saved and no errors are triggered.

The table is in the database and it's completed with all columns, but no data inside

The "isEmpty" function does not get values at all, what am I missing?

Here is the field code

<?php
/**
 * @file
 * Contains \Drupal\magicnet\Plugin\Field\FieldType\Social.
 */

namespace Drupal\magicnet\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;

/**
 * Plugin implementation of the 'social' field type.
 *
 * @FieldType (
 *   id = "social",
 *   label = @Translation("Social"),
 *   description = @Translation("Immagazzina tipo di social e ID social"),
 *   default_widget = "social_widget",
 *   default_formatter = "social_formatter"
 * )
 */
class Social extends FieldItemBase {
  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return array(
      'columns' => array(
        'social_type' => array(
          'description' => 'Social',
          'type' => 'int',
          'length' => 5,
          'not null' => FALSE,
        ),
        'social_id' => array(
          'description' => 'ID del social',
          'type' => 'varchar',
          'length' => 255,
          'not null' => FALSE,

        )
      ),
    );
  }

    /**
     * {@inheritdoc}
     */
    public function isEmpty() {
        $value1 = $this->get('social_type')->getValue();
        $value2 = $this->get('social_id')->getValue();
        return empty($value1) || empty($value2);
    }

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    // Add our properties.
    $properties['social_type'] = DataDefinition::create('integer')
      ->setLabel(t('Seleziona il social'));

    $properties['social_id'] = DataDefinition::create('string')
      ->setLabel(t('ID del social'));



    return $properties;
  }
}

The Widget code

<?php
/**
 * @file
 * Contains \Drupal\magicnet\Plugin\Field\FieldWidget\SocialWidget.
 */

namespace Drupal\magicnet\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\WidgetBase;

/**
 * Plugin implementation of the 'social' widget.
 *
 * @FieldWidget (
 *   id = "social_widget",
 *   label = @Translation("Social widget"),
 *   field_types = {
 *     "social"
 *   }
 * )
 */
class SocialWidget extends WidgetBase {
  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree('social');
    $termini=array();
    foreach($terms as $term){
        $termini[$term->tid]=$term->name;
    }

    $element['social_type'] = array(
      '#type' => 'select',
      '#title' => $this->t('Social'),
      '#options' => $termini
    );
    $element['social_id'] = array(
      '#type' => 'number',
      '#title' => $this->t('Social ID'),
      '#size' => 80
    );


    $element += [
      '#type' => 'fieldset',
    ];

    return array('value' => $element);
  }
}

Formatter code

<?php
/**
 * @file
 * Contains \Drupal\magicnet\Plugin\Field\FieldFormatter\SocialFormatter.
 */

namespace Drupal\magicnet\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;

/**
 * Plugin implementation of the 'social' formatter.
 *
 * @FieldFormatter (
 *   id = "social_formatter",
 *   label = @Translation("Social"),
 *   field_types = {
 *     "social"
 *   }
 * )
 */
class SocialFormatter extends FormatterBase {
  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode = NULL) {
    $elements = array();

    foreach ($items as $delta => $item) {
      $markup = 'Type:'.$item->social_type.', id:'.$item->social_id;

      $elements[$delta] = array(
        '#type' => 'markup',
        '#markup' => $markup,
      );
    }

    return $elements;
  }
}
  • Is there any luck with this ? – Kshitij Soni Oct 11 '17 at 8:53
  • I move on to another solution, but i think the problem is about the definition of the data kind, something does not match so it does not save – Piero Oct 12 '17 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.