1

I'm probably a bit confused by documentation.

Got a custom field with some properties defines in hook_schema() :

/**
 * Some field type.
 *
 * @FieldType(
 *   id = "field_selector",
 *   label = @Translation("Selector"),
 *   description = @Translation("Selector field"),
 *   default_widget = "field_selector_widget",
 *   default_formatter = "coupon_selector_formatter",
 *   list_class = "\Drupal\field_select\Plugin\Field\FieldType\SelectorFieldItemList"
 * )
 */
class SelectorItem extends FieldItemBase implements FieldItemInterface {

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return [
      'columns' => [
        'programmes' => [
          'description' => 'Serialized array of programmes ids.',
          'type' => 'blob',
          'size' => 'normal',
          'serialize' => TRUE,
        ],
        'types' => [
          'description' => 'Serialized array of  types ids.',
          'type' => 'blob',
          'size' => 'normal',
          'serialize' => TRUE,
        ]
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
   public static function defaultFieldSettings() {
     return array(
       'requiredFields' => array(),
       'levels' => \Drupal::config('select.settings')
         ->get('levels'),
     ) + parent::defaultFieldSettings();
   }

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['programmes'] = MapDataDefinition::create('any')
      ->setLabel(new TranslatableMarkup('Programmes'))
      ->addConstraint('comma_separated_string');

    $properties['types'] = MapDataDefinition::create('any')
      ->setLabel(new TranslatableMarkup('Type Ids'))
      ->addConstraint('comma_separated_number');

    return $properties;
  }

  /**
   * {@inheritDoc}
   */
  public function fieldSettingsForm(array $form, FormStateInterface $form_state) {

    $element['requiredFields'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Allowed level(s)'),
      '#options' => array(
        'programmes' => $this->t('Programmes'),
        'types' => $this->t('Types'),
      ),
      '#description' => t('If empty field will not impose any requirement.'),
      '#default_value' => $this->getSetting('requiredFields'),
    );

    $element['levels'] = array(
      '#type' => 'textarea',
      '#title' => t('Level to Label mapping'),
      '#default_value' => $this->getSetting('levels'),
      '#required' => TRUE,
      '#description' => t('Add level|label pair, only 1 level per line')
    );

    return $element;
  }

I would like to alter those field property names to something else, and change some settings also.

What I do and its working (I do not care about content data) is drop previous field columns and add the new ones for the schema and provide configuration update for the settings, but is this correct?

Do I have to use entityDefinitionUpdateManager for the schema update also, or something like this is enough?

 function modulename_update_8001(&$sandbox) {
   $specProgrammes = [
     'description' => 'CHANGED',
     'type' => 'blob',
     'size' => 'normal',
     'serialize' => TRUE,
     'not null' => CHANGED,
   ];

   $specTypes = [
     'description' => 'CHANGED',
     'type' => 'blob',
     'size' => 'normal',
     'serialize' => TRUE,
     'not null' => CHANGED,
   ];

   $schema = Database::getConnection()->schema();
   // Replace field and field revision db fields.
   $schema->dropField('node__field_selector', 'OLD_COLUMN_NAME');
   $schema->dropField('node__field_selector', 'OLD_COLUMN_NAME2');
   $schema->dropField('node_revision__field_selector', 'OLD_COLUMN_NAME');
   $schema->dropField('node_revision__field_selector', 'OLD_COLUMN_NAME2');

   $schema->addField('node__field_selector', 'NEW_COLUMN_NAME', $specProgrammes);
   $schema->addField('node__field_selector', 'NEW_COLUMN_NAME', $specTypes);
   $schema->addField('node_revision__field_selector', 'field_selector_programmes', $specProgrammes);
   $schema->addField('node_revision__field_selector', 'field_selector_types', $specTypes);
    }

Note I don't care about content data to restore or update.

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.