I have added weight field as base field in my custom entity type.
When I create view for this entity type, only original weight field is available in view fields list and selector view field is missing.

Please note that Im facing this problem only in case of base field. Im also using weight field with media entity bundles, but there it works perfectly.

Is the custom entity type support missing from weight module or I am missing something else?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tahirmus created an issue. See original summary.

tahirmus’s picture

Title: weight_selector field missing for custom entity type » Weight module does not work for Drupal 8.3
Priority: Normal » Major
Issue summary: View changes
tahirmus’s picture

Title: Weight module does not work for Drupal 8.3 » weight_selector field missing for custom entity type
Priority: Major » Normal
Issue summary: View changes
ziomizar’s picture

Hi tahirmus,

I've tried it quickly using eck and it seems work correctly, with the latest dev version and drupal 8.6.1.
Did you still have the issue?

ziomizar’s picture

Status: Active » Postponed (maintainer needs more info)
ziomizar’s picture

Issue tags: +Needs tests
ziomizar’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)
lobodakyrylo’s picture

Status: Closed (works as designed) » Needs work

I also tried to create custom entity with weight field. My code looks like this:

/**
 * @ContentEntityType(...)
 */
class CustomEntity extends ContentEntityBase implements ContentEntityInterface {

  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

    // ...
    
    $fields['weight'] = BaseFieldDefinition::create('weight')
      ->setLabel(t('Weight'))
      ->setDefaultValue(0)
      ->setDisplayOptions('form', [
        'type' => 'weight',
      ])
      ->setDisplayConfigurable('form', TRUE);

    return $fields;
  }
  
}

This code doesn't define field storage so the Weight Selector field isn't shown in Views.

I found a solution but it works just for me:

/**
 * Implements hook_views_data_alter().
 */
function custom_entity_views_data_alter(array &$data) {
  $table_data = &$data['custom_entity'];
  $field_name = 'weight';
  
  $table_data[$field_name . '_value']['field'] = $table_data[$field_name]['field'];
  $title = t('@title Selector (@field)', [
    '@title' => $table_data[$field_name]['title'],
    '@field' => $field_name,
  ]);
  $short_title = t('@title Selector', ['@title' => $table_data[$field_name]['title']]);
  $table_data[$field_name]['title'] = $title;
  $table_data[$field_name]['title short'] = $short_title;
  $table_data[$field_name]['field']['id'] = 'weight_selector';
}

This code overrides data for my entity. I think weight_field_views_data should be rewritten to cover fields without storage.

lobodakyrylo’s picture

Also, I noticed that not all entities could have langcode field and it could generate this error:

Notice: Undefined property: Drupal\views\ResultRow::$customizer_style__weight_langcode in Drupal\weight\Plugin\views\field\WeightSelector->viewsForm() (line 89 of modules/contrib/weight/src/Plugin/views/field/WeightSelector.php).

This is a patch for solving this issue

ziomizar’s picture

Status: Needs work » Needs review

set to "Need review" to run automatic tests

  • lobodacyril authored 0bc7cd5 on 8.x-3.x
    Issue #2887710 by lobodacyril: weight_selector field missing for custom...
ziomizar’s picture

Status: Needs review » Needs work

Thanks for spotting the error in #9 this has been commited to dev,

The issue still open as the patch don't solve the problem in the IS.

Rithesh BK’s picture

Assigned: Unassigned » Rithesh BK

currently i am working on that ......

Rithesh BK’s picture

Assigned: Rithesh BK » Unassigned