using hook_element_info()

Comments

artusamak’s picture

What would we get that we don't already have?

energee’s picture

I was looking to use this in a custom form

mike.davis’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new3.1 KB

I have had need to use this field in a custom form using FAPI, so was after this feature as well.

This patch enabled me to use the defined field 'colorfield_picker' to add a colour picker field using FAPI. It also adds a field for 'colorfield_rgb' so that you can add the RGB field to a form if you wish using FAPI.

RaphaelBriskie’s picture

StatusFileSize
new4.18 KB

Patch for using the colorfield_minicolors module with the form API.

lambic’s picture

What's the minimum needed to make this work as a FAPI element? I tried with this:

    $form['global']['text_colour'] = array(
      '#type' => 'colorfield_picker',
      '#title' => 'Text Colour',
    );

but it didn't do anything.

lolandese’s picture

@lambic:
Try to add something along the lines of '#default_value' => variable_get('text_colour', '#000'),?

It would be good if a code example for the right implementation is added to the README.txt.

lambic’s picture

Got this working, turned out to be an unrelated issue, the example in #5 does work.

lambic’s picture

Status: Needs review » Reviewed & tested by the community
lambic’s picture

Spoke too soon, doesn't look like it recognises #default_value. With this:

$form['global_pickers']['letter_text_colour'] = array(
    '#type' => 'colorfield_picker',
    '#title' => 'Primary Text Colour',
    '#default_value' => '#ea4e4e',
);

The field does not get populated with the default value.

lambic’s picture

Status: Reviewed & tested by the community » Needs review
lolandese’s picture

Use the suggestion of #6. If not, how are you going to populate the field with the value that was submitted in your form the last time?

I tested and it is working fine. I agree that '#default_value' is a bit misleading as a name. See https://api.drupal.org/api/drupal/developer%21topics%21forms_api_referen... for more info.

lambic’s picture

not sure I understand, what is the problem with my example in #9? Can you show your working example?

lolandese’s picture

What is the problem with my example in #9?

How do you store the submitted value and reproduce it on a reopened form?

Can you show your working example?

    $form['global']['text_colour'] = array(
      '#type' => 'colorfield_picker',
      '#title' => 'Text Colour',
      '#default_value' => variable_get('text_colour', '#000'),
    );
lambic’s picture

hmm that's what I have except I hard-code the default for testing, not sure why mine isn't working then.

lolandese’s picture

Based on your example of #9 it should look like:

$form['global_pickers']['letter_text_colour'] = array(
    '#type' => 'colorfield_picker',
    '#title' => 'Primary Text Colour',
    '#default_value' => variable_get('letter_text_colour', '#ea4e4e'),
);

Note that the variable name should be exactly the same as the $form array key to work as expected.

lambic’s picture

I'm getting my data from elsewhere, not from a variable, but otherwise I'm doing exactly what you're doing. Will dig deeper to try to figure out why I'm getting different results.

lolandese’s picture

I'm getting my data from elsewhere, not from a variable ..

Do you have a code example from an existing core or contrib module that uses your same method to store and retrieve form data?

lambic’s picture

Here's my code:

foreach ($templates->colourPickers() as $field => $title) {
  $form['global_pickers'][$field] = array(
    '#type' => 'colorfield_picker',
    '#title' => t($title),
    '#default_value' => isset($pickers[$field]) ? $pickers[$field] : '#ffffff',
  );
}
dsm($form['global_pickers']);

The dsm gives me:

field_letter_background_colour (Array, 3 elements)

    #type (String, 17 characters ) colorfield_picker
    #title (String, 29 characters ) Tool Background Colour
    #default_value (String, 7 characters ) #a21717

But the field appears empty when the form is rendered.

lambic’s picture

Got this working finally, with this:

    foreach ($templates->colourPickers() as $field => $title) {
      $form['global_pickers'][$field] = array(
        '#type' => 'colorfield_picker',
        '#title' => t($title),
        '#default_value' => array('colorfield_picker' => isset($pickers[$field]) ? $pickers[$field] : ''),
      );
    }
lambic’s picture

Status: Needs review » Reviewed & tested by the community
lolandese’s picture

@lambic: Thanks for your feedback on how to get it working when used with a field (not a settings form). I will make sure your example gets included in the documentation and/or README.txt and commit the supplied patch above soon.

  • RaphaelBriskie authored f041181 on 7.x-1.x
    Issue #1973194 by RaphaelBriskie, mike.davis, lambic: Add form_api hook
    
lolandese’s picture

Status: Reviewed & tested by the community » Fixed

Also included an example in the README.txt.

Status: Fixed » Closed (fixed)

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