Smartdate does not work on Custom Entities generated by Drupal Console.

I added a custom field:

    $fields['any_name'] = BaseFieldDefinition::create('smartdate')
      ->setLabel(t('Smart Date'))
      ->setRevisionable(TRUE)
      ->setDisplayOptions('view', [
        'type' => 'smartdate_default',
        'label' => 'above',
        'weight' => 7,
      ])
      ->setDisplayOptions('form', array(
        'type' => 'smartdate_default',
        'weight' => -4,
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

During installation, I did not get any errors.
I opened add-form for my custom entity and got:

The website encountered an unexpected error. Please try again later.

And in the log messages I have this error:

Error: Call to undefined method Drupal\Core\Field\BaseFieldDefinition::getThirdPartySetting() in Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase->formElement() (line 29 of ../web/modules/contrib/smart_date/src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php)

Am I missing some configurations in Display Options for view and form?

Comments

alvarito75 created an issue. See original summary.

alvarito75’s picture

Issue summary: View changes
mandclu’s picture

Status: Active » Needs review
StatusFileSize
new2.54 KB

Please try the attached patch. Note that if you want to use the recurring date functionality provided by smart_date_recur you need to explicitly declare that your field allows recurring dates, which you would do by adding ->setSetting('allow_recurring', TRUE) to your field definition code.

alvarito75’s picture

Thanks mandclu.

I applied the patch and get this error:

TypeError: Argument 3 passed to Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget() must be of the type array, null given, called in /web/modules/contrib/smart_date/src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php on line 78 in Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget() (line 90 of /web/modules/contrib/smart_date/src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php)

I had to add the proper use statements though.

mandclu’s picture

StatusFileSize
new3.98 KB

This patch adds use statements, and also sets defaults for the field widget's default_duration_increments and default_duration. If you are setting these and they're not getting passed through properly, it's possible they're returned by BaseFieldDefinition in a different structure than FieldConfigInterface, in which case it would be helpful if you could post a dump (var_dump or kint or similar) of the value you see from $this->fieldDefinition->getDefaultValueLiteral() within SmartDateWidgetBase->formElement().

alvarito75’s picture

Hi, Martin, I applied the patch and it did not work though.

This is the output I got:

TypeError: Argument 3 passed to Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget() must be of the type array, null given, called in web/modules/contrib/smart_date/src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php on line 79 in Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget() (line 91 of web/modules/contrib/smart_date/src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php)

And the output from

$this->fieldDefinition->getDefaultValueLiteral()
^ null
The website encountered an unexpected error. Please try again later.

I attached two images.

And I also changed this:

public static function createWidget(&$element, $values, array $defaults) {

to this, I removed the array value.

public static function createWidget(&$element, $values, $defaults) {

after doing this I could see the add-form for my entity, but it did not save the entity, so I got a different error:

Error: Call to undefined method Drupal\Core\Field\BaseFieldDefinition::getThirdPartySetting() in Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase->massageFormValues() (line 211 of /web/modules/contrib/smart_date/src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php)

FYI, I'm still using the same structure above.

$fields['any_name'] = BaseFieldDefinition::create('smartdate')
      ->setLabel(t('Smart Date'))
      ->setRevisionable(TRUE)
      ->setDisplayOptions('view', [
        'type' => 'smartdate_default',
        'label' => 'above',
        'weight' => 7,
      ])
      ->setDisplayOptions('form', array(
        'type' => 'smartdate_default',
        'weight' => -4,
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

And I created my custom entity with Drupal console and the entity has bundles.

mandclu’s picture

Assigned: Unassigned » mandclu
Status: Needs review » Needs work
dwkitchen’s picture

EDIT: Yes same issue can load the form but can't save the entity, patch on the way.

Here is my field definition in baseFieldDefinitions() for my entity

$fields['meeting_datetime'] = BaseFieldDefinition::create('smartdate')
      ->setLabel(t('Meeting Date'))
      ->setDescription(t('The start and optional end time of the meeting'))
      ->setRequired(TRUE)
      ->setDefaultValue([
        'default_date_type' => 'next_hour',
        'default_date' => '',
        'default_duration_increments' => "0 | No End Time\r\n30\r\n60|1 hour\r\n90\r\n120|2 hours\r\ncustom",
        'default_duration' => '60',
      ])
      ->setDisplayOptions('form', [
        'type' => 'smartdate_timezone',
        'weight' => -3,
        'settings' => [
          'modal' => TRUE,
          'default_tz' => 'user',
        ]
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
dwkitchen’s picture

StatusFileSize
new5.06 KB

I don't really like that the FieldWidget is checking for the ThirdParty setting in the main definition.

I'm sure the Smart Date Recur module should be altering the Widget. However here is a widget that allows the recur setting in both the render and the save.

dwkitchen’s picture

Status: Needs work » Needs review
mandclu’s picture

I see what you mean, but as much as I could I wanted to keep the code to support recurring functionality self contained in its own submodule. The code in this latest patch looks good to me, but I don't have a custom entity handy to test this against.

If someone could either move this to RTBC or provide an entity definition I can use to test it, I would be happy to get this merged in.

Also, I greatly appreciate your fix here, but this is a good example of where including an interdiff would have been helpful.

mandclu’s picture

Assigned: mandclu » Unassigned
niklan’s picture

Status: Needs review » Reviewed & tested by the community

I can confirm that this error occurs on custom entities. Patch solve the problem.

  • mandclu committed 983a6b1 on 8.x-2.x authored by dwkitchen
    Issue #3114330 by mandclu, dwkitchen, alvarito75, Niklan: Smartdate does...
mandclu’s picture

Status: Reviewed & tested by the community » Fixed

Thanks everyone for helping to move this to completion. Merged in, should have a new release that includes this shortly.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.