1

I have a text field in a content type in the admin. This week I only like people to fill in, say the words yes, no or I forgot. I don't want to create a new field type. Next week, I will implement yet other choices or maybe I'll just give them the plain text widget back.

Why does this not work? I doesn't even show up as a widget for a text field?

     /**
     * Implements hook_field_widget_info().
     */
    function MYWIDGET_field_widget_info() {
      return array(
        'MYWIDGET_field' => array(
          'label' => t('MYWIDGETNAME'),
          'field types' => array('text'),
        ),
      );
    }
    /**
     * Implements hook_field_widget_form().
     */
    function MYWIDGET_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
      switch ($instance['widget']['type']) {
        case 'MYWIDGET_field' :
          $element['MYWIDGETVALUE'] = array(
            '#type' => 'select',
            '#title' => t('MYWIDGETLABEL'),
            '#default_value' => isset($items[$delta]['MYWIDGETVALUE']) ? $items[$delta]['MYWIDGETVALUE'] : '',
            '#options' => array(
              '1' => 'Yes',
              '-1' => 'No',
              '0' => 'I forgot',
            ),
            '#required' => $element['#required'],
          );
          break;
      }
      return $element;
    }
  • I'm not sure why your widget is not showing up, But I do know that unless you implement hook_field_is_empty() and at the very least return True in that function, the content will never save. – Graham May 18 '17 at 15:40

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.