1

I have created a module for adding a custom field. I works fine locally. But when I move it to another server the custom field I created gets categorized under disabled fields. What could be the issue? Is that anything related to permissions? Since it works as per my requirement locally I hope the code is fine.

CertField.php as


namespace Drupal\certfield\Plugin\Field\FieldType;

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

/**
 * Plugin implementation of the 'address' field type.
 *
 * @FieldType(
 *   id = "CertField",
 *   label = @Translation("CertField"),
 *   description = @Translation("certfields."),
 *   category = @Translation("Custom"),
 *   default_widget = "CertFieldWidget",
 *   default_formatter = "CertFieldFormatter"
 * )
 */
class CertField extends FieldItemBase {

  /**
   * Field type properties definition.
   * 
   * Inside this method we defines all the fields (properties) that our 
   * custom field type will have.
   * 
   * Here there is a list of allowed property types: https://goo.gl/sIBBgO
   */
  public static function propertyDefinitions(StorageDefinition $storage) {

    $properties['privatekey'] = DataDefinition::create('string')
      ->setLabel(t('Private Key'));

    $properties['csr'] = DataDefinition::create('string')
      ->setLabel(t('Cerificate Signing Request'));

    $properties['sercer'] = DataDefinition::create('string')
      ->setLabel(t('Server Cerificate'));
    return $properties;
  }

  /**
   * Field type schema definition.
   * 
   * Inside this method we defines the database schema used to store data for 
   * our field type.
   * 
   * Here there is a list of allowed column types: https://goo.gl/YY3G7s
   */
  public static function schema(StorageDefinition $storage) {

     $columns = [];
    $columns['privatekey'] = [
      'type' => 'char',
      'length' => 255,
    ];
    $columns['csr'] = [
      'type' => 'char',
      'length' => 255,
    ];
    $columns['sercer'] = [
      'type' => 'char',
      'length' => 255,
    ];

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

  /**
   * Define when the field type is empty. 
   * 
   * This method is important and used internally by Drupal. Take a moment
   * to define when the field fype must be considered empty.
   */
  public function isEmpty() {
$isEmpty = 
      empty($this->get('privatekey')->getValue()) &&
      empty($this->get('csr')->getValue()) && 
       empty($this->get('sercer')->getValue()) ;

    return $isEmpty;
  }

} // class

and my CertFieldWidget.php is as



namespace Drupal\certfield\Plugin\Field\FieldWidget;

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

/**
 * Plugin implementation of the 'CertFieldWidget' widget.
 *
 * @FieldWidget(
 *   id = "CertFieldWidget",
 *   label = @Translation("CertField select"),
 *   field_types = {
 *     "CertField"
 *   }
 * )
 */
class CertFieldWidget extends WidgetBase {

  /**
   * Define the form for the field type.
   * 
   * Inside this method we can define the form used to edit the field type.
   * 
   * Here there is a list of allowed element types: https://goo.gl/XVd4tA
   */
  public function formElement(
    FieldItemListInterface $items,
    $delta, 
    Array $element, 
    Array &$form, 
    FormStateInterface $formState
  ) {

    $element['privatekey'] = [
      '#type' => 'textfield',
      '#title' => t('Private Key'),
      '#placeholder' => t('Private Key'),
    ];

    // City

    $element['csr'] = [
      '#type' => 'textfield',
      '#title' => t('Certificate Signing Request'),
      '#placeholder' => t('Certificate Signing Request'),
    ];
    $element['sercer'] = [
      '#type' => 'textfield',
      '#title' => t('Server Certificate'),
      '#placeholder' => t('Server Certificate'),
    ];

    return $element;
  }

} // class

My CertFieldFormatter.php is as follows


namespace Drupal\certfield\Plugin\Field\FieldFormatter;

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

/**
 * Plugin implementation of the 'CertFieldFormatter' formatter.
 *
 * @FieldFormatter(
 *   id = "CertFieldFormatter",
 *   label = @Translation("CertField"),
 *   field_types = {
 *     "CertField"
 *   }
 * )
 */
class CertFieldFormatter extends FormatterBase {

  /**
   * Define how the field type is showed.
   * 
   * Inside this method we can customize how the field is displayed inside 
   * pages.
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {


    $elements = [];
    foreach ($items as $delta => $item) {
      $elements[$delta] = [
        '#type' => 'markup',
        '#markup' => $item->privatekey . ', ' . $item->csr
      ];
    }

    return $elements;
  }

} // class
  • Is this a duplicate of the same question you asked 2 days ago? drupal.stackexchange.com/questions/210801/… – mradcliffe Aug 10 '16 at 12:17
  • I have corrected that code issues but this is something related to Permission I think. – Jitha M Saroj Aug 10 '16 at 12:19
  • Okay, just checking. It looked remarkably similar. – mradcliffe Aug 10 '16 at 12:20
  • Can you share the code for the module? – Matt Aug 10 '16 at 17:59
  • When I checked my error log I found one error that 'Class name must be a valid object or a string in Drupal\core\modules\field\src\FieldStorageConfigStorage.php on line 155' – Jitha M Saroj Aug 11 '16 at 5:53

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.