Hello, Drupal newbie here.

For a drupal site we're setting up, we need a field group that consists of a long text (similar to node body) field and an image field. I've gathered that the only way to get that in D7 AND allow multiple values of this field group is to create my own custom field module.

I think I've gotten familiar with the basics, and have succeeded in creating a compound field module, but with simple textfields instead of the required long text and image fields. How do I go about changing these fields to what I need? Am I correct in assuming this should happen in hook_field_widget_form()?

I'd be grateful for any kind of assistance or links to documentation that might help me (I haven't managed to find anything relevant to exactly what I'm doing, most examples I see only use simple text fields). Thanks in advance!

Comments

olrandir’s picture

Can anyone help me on this? Even if you only point me to examples of something similar to my issue (e.g. anyone else who's created a compound field via module for Drupal 7), I'd be very grateful.

mattsqd’s picture

I was trying to figure this out for Drupal 8 and came upon this thread. If you're implementing a 'FieldType' plugin, for the column that will hold the data saved from a text_format widget, add 'serialize' => TRUE to its entry. For example:

'type' => 'text',
'size' => 'normal',
'not null' => FALSE,
'serialize' => TRUE,

The only other thing that might get you is for the propertyDefinitions() for that column, it can't be a DataDefinition::create('string'). That will cause it to throw errors about 'Primitives' because it will hate that it's getting an array. I'm not entirely sure what the best DataDefinition to use but I got it to work with MapDataDefinition::create() and DataDefinition::create('any').

WorldFallz’s picture

iiuyc, just use a 'textarea' for your form element instead of 'textfield'.

olrandir’s picture

Thanks, I now have a larger textfield I can work with! It's not full-featured though, how can I get it to use the wysiwyg (CKEditor) I've defined for use with the default body fields (in Article etc.)?

Are there any links to documentation for these things? I tried changing 'textfield' to 'longtext' and various others (guesswork, obviously), but I'd never have guessed 'textarea' if you hadn't said it.

Thanks again!

WorldFallz’s picture

...how can I get it to use the wysiwyg (CKEditor) I've defined for use with the default body fields (in Article etc.)?

Depends on whether or not your using the ckeditor or wysiwyg module and how you enable it for existing fields-- you'll need to check the docs for the module you're using.

Are there any links to documentation for these things?

Usually... in this case the forms api reference is at: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.....

olrandir’s picture

Thanks for the api ref link, hadn't found that one. Very enlightening!

I'm using the CKEditor standalone module, I'll have a look in the docu and return to mention my findings in case any one else has the same issue.

Thanks for taking the time to help me! :)

olrandir’s picture

For anyone else having this problem, my $element array in hook_field_widget_form is as follows:

$element['myfield'] = array(
		'#module' => 'text',
        '#settings' => array(
          'rows' => 20,
        ),
		'#type'=>'text_format',
		'#title' => t('My field'),
		'#rows' => 20,
		'#default_value' => isset($items[$delta]['body']) ? $items[$delta]['body'] : NULL,
		'#format'=>'filtered_html',
	);

CKEditor appears and works perfectly with my long text field now (of course, I've set CKEd to work with filtered html fields, through its own config panel).

Many thanks to WorldFallz for pointing me in the direction of the Forms API documentation, which contained the answers I needed!

WorldFallz’s picture

'#type'=>'text_format'

Are you sure that's correct? Afaik, there is no 'text_format' #type (http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....) and it should be:

'#type'=>'textarea',
'#text_format' => isset($edit->format) ? $edit->format : FILTER_FORMAT_DEFAULT),

So if that's not correct, then we should create an issue to have api.drupal.org fixed.

chevron’s picture

olrandir’s picture

Yes, I'm sure, it works that way for me. I tried the code snippet you quoted (ran across it at some point when I was still trying to get it to work), and got error messages complaining that FILTER_FORMAT_DEFAULT was undefined, if I remember correctly -in any case, it didn't work.

Chevron's link is up to date (thx, Chevron!).

WorldFallz’s picture

yep-- see the link chevron provided above-- seems the api quick ref is outdated.

bjuncosa’s picture

What did you your schema look like for this to work? I can successfully create / update a compound field that uses textfield or textarea, but things break once I move over to text_format.

chevron’s picture

This was my experience, too. I only have this error with text_format.

nsteen’s picture

I created a field in the schema, but Drupal doesn't seem to understand that it should use that field to store the format in.
You can see that happening in the PDO dump on the error, one value too much is defined, but the value for the format is empty...

Any thoughts? I really have to get this working!

stella’s picture

I have the same problem. Getting the error:

PDOException: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1: INSERT INTO {field_data_field_sections} (entity_type, entity_id, revision_id, bundle, delta, language, field_sections_title, field_sections_text, field_sections_format) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7_value, :db_insert_placeholder_7_format, :db_insert_placeholder_8); Array ( [:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => 30 [:db_insert_placeholder_2] => 30 [:db_insert_placeholder_3] => test [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6] => sdf [:db_insert_placeholder_8] => [:db_insert_placeholder_7_value] => sdfsdfsdf dfdsf [:db_insert_placeholder_7_format] => filtered_html ) in field_sql_storage_field_storage_write() (line 425 of modules/field/modules/field_sql_storage/field_sql_storage.module).
stella’s picture

Thanks to some help from sun on IRC, we figured out this solution:

function mymodule_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  if ($field['type'] == 'my_compound_field_type') {
    foreach ($items as $delta => $item) {
      // I had a textarea field called "text". My schema definition had cols "text" and "format".
      $items[$delta]['format'] = $item['text']['format'];
      $text = $item['text']['value'];
      $items[$delta]['text'] = $text;
    }
  }
}
serverjohn’s picture

stella's solution works. The problem is that the text_format returns and array. The text and the format used. If you don't stripe down the array as the above solution does you will get an error when you try to save it. This function would go in your .module file.

rafinskipg’s picture

espurnes’s picture

The above link is broken. I found a new link working: http://rvpg.me/monchacos/code/hookfieldschema-textformat-textarea-drupal

sr631’s picture

Thanks so much for this solution - I've been searching for help to do almost the exact same thing. I can now get the textarea to work as needed - but not the image field. Can you share any help with that? I'd love to see an example project with complex fields like this rather than just the two textfields! Any help would be great.

RumpledElf’s picture

Yeah, a couple of people further up mentioned they've got an image field working in a compound field and there's a dearth of examples of this.

bkudrle’s picture

I know this is a rather old thread, but it comes up in search for something that I was looking for that I did not find an answer for in any other thread and that seems to be implied in the original question. Although the information here seems to have been satisfactory according to the comments, my need and perhaps that of others was to create a 'longtext' type of field in the database for a custom module that I am building for Drupal 7. I found that in order to get a type of longtext for the data type, I needed to use "text_long" as the "type" for the schema definition. The "text" type along with the "textarea" for the installed instances will just give a varchar of 255 by default for the datatype in the database.

For example, I have the following working correctly for a custom content type module...

[in mymodule.install file]

function mymodule_install()
{
  ...
  foreach (_mymodule_installed_fields() as $field)  {
    field_create_field($field);
  }
  ...
}

function _mymodule_installed_fields()
{
  return array(
    ...
    'mybigfield' => array(
      'label' => 'Big Label',
      'description' => 'Description for the Big Field',
      'field_name' => 'mybigfield',
      'type' => 'text_long',
    ),

    ...
  );
}

This then will create a longtext type in the database for the field mybigfield (tested only in MySQL). The use of 'textarea' as delineated in the comments in this thread will then allow the interface to be a textarea with multiple lines.

Hope that helps someone. I got this hint by looking at the core module at /modules >> field >> modules >> text >> text.install. That is where the text_long is defined.