2

How can I use a the full_html format for my custom field in my custom node type? I'd prefer a solution that didn't use any third-party modules.

I've essentially followed this guide, but my custom fields have their HTML tags escaped when rendered.

I've been searching for hours, and can't seem to quite come up with a solution.

I've added 'format' => 'full_html' like in this answer: Specifying text format when creating field on custom node type, but it didn't do anything.

After my changes I used the Devel module to uninstall and reinstall my module.

Edit: added code.

biolawnorganics_content_types.install:

<?php

require_once 'include/nodetypes.inc';

function _biolawnorganics_add_missing_fields() {
  node_types_rebuild();

  $nodeTypes = node_type_get_types();
  $fieldInfoMap = field_info_field_map();

  $fieldsArr = _biolawnorganics_build_all_fields();
  $fieldsInstancesArr = $fieldsArr['instance'];
  $fieldsTypesArr = $fieldsArr['type'];

  foreach ($fieldsTypesArr as $ft) {
    $fieldName = $ft['field_name'];

    if (!isset($fieldInfoMap[$fieldName]))
      field_create_field($ft);
  }

  $contentTypesArr = _biolawnorganics_build_all_content_types($fieldsInstancesArr);
  $contentInfosArr = $contentTypesArr['info'];
  $contentFieldsArr = $contentTypesArr['fields'];

  foreach ($contentFieldsArr as $ck => $cfs) {
    if (!isset($nodeTypes[$ck])) {
      error_log("WARNING: Content type mismatch on $ck.");
      continue;
    }

    $bodyFieldInstance = field_info_instance('node', 'field_body', $ck);

    if (is_null($bodyFieldInstance))
      node_add_body_field($nodeTypes[$ck]); 

    foreach ($cfs as $cf) {
      $fieldInstance = field_info_instance('node', $cf['field_name'], $ck);

      if (is_null($fieldInstance))
        field_create_instance($cf);
    }
  }
}

function biolawnorganics_content_types_install() {
  _biolawnorganics_add_missing_fields();

  drupal_set_installed_schema_version('biolawnorganics_content_types', 7001);
}

function biolawnorganics_content_types_update_7001(&$sandbox) {
  biolawnorganics_content_types_install();
}

function biolawnorganics_content_types_uninstall() {
  $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';

  foreach (array_keys(_BIOLAWNORGANICS_CONTENT_TYPES) as $t) {
    $result = db_query($sql, array(':type' => $t));

    $nodeIds = array();

    foreach ($result as $r)
      array_push($nodeIds, $r->nid);

    node_delete_multiple($nodeIds);
    node_type_delete($t);
  }

  foreach (array_keys(_BIOLAWNORGANICS_FIELDS) as $f) {
    $fieldInstances = field_info_instances('node', $f);

    foreach ($fieldInstances as $fi)
      field_delete_instance($fi);

    field_delete_field($f);
  }
}

// vim: set ts=2 sw=2 et syn=php:

include/nodetypes.inc:

<?php

define(
  '_BIOLAWNORGANICS_FIELDS', array(
    'biolawnorganics_promos'  => array(
      'type' => array(
        'label'  => 'Promotions Sidebar',
        'type'   => 'text_long',
        'format' => 'full_html'
      ),
      'instance'  => array(
        'widget'  => array(
          'type'   => 'text_textarea',
          'format' => 'full_html'
        )
      )
    )
  )
);
define(
  '_BIOLAWNORGANICS_CONTENT_TYPES', array(
    'biolawnorganics_home'  => array(
      'info'    => array(
        'name'        => 'bioLawn Organics Home',
        'description' => 'bioLawn Organics Home Page',
        'has_title'   => FALSE
      ),
      'fields'  => array(
        'biolawnorganics_promos'  => NULL
      )
    ),
    'biolawnorganics_page'  => array(
      'info'    => array(
        'name'        => 'bioLawn Organics Page',
        'description' => 'bioLawn Organics Generic Page',
        'has_title'   => TRUE,
        'title_label' => 'Title'
      ),
      'fields'  => array(
        'biolawnorganics_promos'  => NULL
      )
    )
  )
);

function _biolawnorganics_build_field_type($fieldName, $fieldTypePartial) {
  $fieldType = $fieldTypePartial;

  $t = get_t();

  $fieldType['field_name']  = $fieldName;
  $fieldType['label']       = $t($fieldType['label']);

  return $fieldType;
}

function _biolawnorganics_build_field_instance($fieldType, $fieldInstancePartial) {
  $fieldInstance = $fieldInstancePartial;

  $fieldName = $fieldType['field_name'];

  #$t = get_t();

  $fieldInstance['field_name']  = $fieldName;
  $fieldInstance['label']       = $fieldType['label'];
  $fieldInstance['type']        = $fieldType['type'];
  $fieldInstance['entity_type'] = 'node';

  $fieldInstance['display'] = array(
    'example_node_list' => array(
      'label' => $fieldType['label'],
      'type'  => $fieldType['type']
    )
  );

  return $fieldInstance;
}

function _biolawnorganics_build_content_info($contentName, $contentInfoPartial) {
  $contentInfo    = $contentInfoPartial;

  $t = get_t();

  $contentInfo['base']          = $contentName;
  $contentInfo['name']          = $t($contentInfo['name']);
  $contentInfo['description']   = $t($contentInfo['description']);
  if ($contentInfo['has_title'])
    $contentInfo['title_label'] = $t($contentInfo['title_label']);

  return $contentInfo;
}

function _biolawnorganics_build_content_fields(
    $contentName,
    $contentFieldsPartial,
    $fieldInstancesArr) {
  $contentFields = $contentFieldsPartial;

  foreach (array_keys($contentFields) as $fn) {
    $fieldInstance = $fieldInstancesArr[$fn];

    if (is_null($fieldInstance)) {
      error_log("WARNING: Missing field instance for $fn.");
      continue;
    }

    $fieldInstance = (array)clone (object)$fieldInstance;

    $fieldInstance['bundle'] = $contentName;

    $contentFields[$fn] = $fieldInstance;
  }

  return $contentFields;
}

function _biolawnorganics_build_all_fields() {
  $fieldTypesArr = array();
  $fieldInstancesArr = array();

  foreach (_BIOLAWNORGANICS_FIELDS as $fk => $fv) {
    $ft = _biolawnorganics_build_field_type($fk, $fv['type']);
    $fi = _biolawnorganics_build_field_instance($ft, $fv['instance']);

    $fieldTypesArr[$fk] = $ft;
    $fieldInstancesArr[$fk] = $fi;
  }

  $fieldsArr = array(
    'type' => $fieldTypesArr,
    'instance' => $fieldInstancesArr
  );

  return $fieldsArr;
}

function _biolawnorganics_build_all_content_types($fieldInstancesArr) {
  $contentInfosArr = array();
  $contentFieldsArr = array();

  foreach (_BIOLAWNORGANICS_CONTENT_TYPES as $ck => $cv) {
    $ci = _biolawnorganics_build_content_info($ck, $cv['info']);
    $cf = _biolawnorganics_build_content_fields($ck, $cv['fields'], $fieldInstancesArr);

    $contentInfosArr[$ck] = $ci;
    $contentFieldsArr[$ck] = $cf;
  }

  $contentTypesArr = array(
    'info' => $contentInfosArr,
    'fields' => $contentFieldsArr
  );

  return $contentTypesArr;
}

// vim: set ts=2 sw=2 et syn=php:
  • can you add some piece of code? – Adrian Cid Almaguer Dec 30 '16 at 20:20
  • @AdrianCidAlmaguer Yes, and I did, but it's very long and would be a pain to trim down very much. – Isabell Cowan Dec 30 '16 at 20:28
  • I can't see the 'format' => 'full_html' in your code – Adrian Cid Almaguer Dec 30 '16 at 20:30
  • @AdrianCidAlmaguer Your right, I had removed it when I checked back out master (to remove all my breadcrumbs), but I put it in both the _BIOLAWNORGANICS_FIELDS['biolawnorganics_promos']['type'] and _BIOLAWNORGANICS_FIELDS['biolawnorganics_promos']['instance'] arrays. I'll edit my post now. – Isabell Cowan Dec 30 '16 at 20:38
1

If you want to have a text with full_html format you can use the text_format like this:

  $form['comment'] = array(
    '#type' => 'text_format',
    '#base_type' => 'textarea', // #base_type is optional.
    '#title' => t('Comment'),
    '#default_value' => $default,
    '#format' => isset($edit['format']) ? $edit['format'] : NULL,
  );
  • I'm not really sure where $form comes from, I'm pretty sure it's not defined this context. However, I changed _BIOLAWNORGANICS_FIELDS['biolawnorganics_promos']['type'] to 'text_format' and get (unsurprisingly) FieldException: Attempt to create a field of unknown type <em class="placeholder">text_format</em>. in field_create_field() (line 110 of /srv/http/drupal/modules/field/field.crud.inc). – Isabell Cowan Dec 30 '16 at 21:04
  • I'm sorry if I'm "just not getting it", I hadn't even heard of Drupal until last week. – Isabell Cowan Dec 30 '16 at 21:09
  • 1
    @IsabellCowan can you check this: drupal.org/node/2031543#comment-7605995 – Adrian Cid Almaguer Dec 30 '16 at 21:12
1

Thank you for your help @AdrianCidAlmaguer, I was just missing 'settings' => array('text_processing' => 1), all 'format' => 'my_format' does is set the default text_processing mode, and has no apparent effect without 'settings' => array('text_processing' => 1). I used this diff and it just worked (I knew it had to be something simple.):

diff --git a/include/nodetypes.inc b/include/nodetypes.inc
index 02d18e5..ad6b2a3 100644
--- a/include/nodetypes.inc
+++ b/include/nodetypes.inc
@@ -4,8 +4,12 @@ define(
   '_BIOLAWNORGANICS_FIELDS', array(
     'biolawnorganics_promos'  => array(
       'type' => array(
-        'label' => 'Promotions Sidebar',
-        'type'  => 'text_long'
+        'label'     => 'Promotions Sidebar',
+        'type'      => 'text_long',
+        'format'    => 'full_html',
+        'settings'  => Array(
+          'text_processing' => 1  // Allow text processing
+        )
       ),
       'instance'  => array(
         'widget'  => array(
@@ -170,9 +174,16 @@ function _biolawnorganics_build_field_instance($fieldType, $fieldInstancePartial
   $fieldInstance['field_name']  = $fieldName;
   $fieldInstance['label']       = $fieldType['label'];
   $fieldInstance['type']        = $fieldType['type'];
+
+  if (isset($fieldType['format']))
+    $fieldInstance['format`']   = $fieldType['format'];
+  
+  if (isset($fieldType['settings']))
+    $fieldInstance['settings']  = $fieldType['settings'];
+
   $fieldInstance['entity_type'] = 'node';

-  $fieldInstance['display'] = array(
+  $fieldInstance['display']     = array(
     'example_node_list' => array(
       'label' => $fieldType['label'],
       'type'  => $fieldType['type']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.