0

I'm writing my first custom field type. The field will contain a rating field for authors. Now when I create the class for the field and activate my module nothing has been found. No errors can be found in the log and the field is not be inserted into the field list of the content type.

I used the "Drupal 8 Development Cookbook" for more information about Drupal 8, but when I check my source with the example from the book I can't find any changes (except from my custom rating).

My class that contains the custom field was placed in the src/Plugin/Field/FieldType/Rating.php file and looks like:

<?php

/**
 * @file
 * Contains \Drupal\RateField\Plugin\Field\FieldType\Rating.
 */

namespace Drupal\RateField\Plugin\Field\FieldType;

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

/**
 * Plugin implementation of the 'Rating' field type.
 *
 * @FieldType(
 *   id = "rating",
 *   label = @Translation("Rating field"),
 *   description = @Translation("This field stores a rating of the object."),
 *   category = @Translation("General"),
 *   default_widget = "string_textfield",
 *   default_formatter = "string"
 * )
 */
class Rating extends FieldItemBase {
  /**
   * {@inheritdoc}
   */
  public static function schema(\Drupal\Core\Field\FieldStorageDefinitionInterface $field_definition) {
    return array(
      'columns' => array(
        'rating' => array(
          'description' => 'Rating',
          'type' => 'int',
          'length' => '255',
          'not null' => TRUE,
          'default' => '0',
        ),
      ),
      'indexes' => array(
        'rating' => array('rating'),
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['rating'] = \Drupal\Core\TypedData\DataDefinition::create('range')
      ->setLabel(t('rating'));

    return $properties;
  }

}

Can someone help me with this?

  • What is your main module's *info.yml called? – Stefanos Petrakis Dec 31 '16 at 11:29
2

This is most probably a namespace issue, I think you have to replace

namespace Drupal\RateField\Plugin\Field\FieldType;

with

namespace Drupal\rate_field\Plugin\Field\FieldType;

or whatever your name's folder is called.

Good luck!

| improve this answer | |
  • My folder is called 'RateField' – JimmyD Dec 31 '16 at 10:12
  • 1
    It is sort of a convention to write module names in all lowercase, so you might want to stick to that. – Neograph734 Dec 31 '16 at 10:43
  • Didn't know that. I changed the module name to lowercase and now the rate field has been found. Tnx – JimmyD Dec 31 '16 at 11:41
  • Great, here is the official reference => drupal.org/docs/8/creating-custom-modules/… – Stefanos Petrakis Dec 31 '16 at 11:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.