Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I need to define a new input format programmatically, probably as part of my module's hook_install(). I need to write code that duplicates the manual operations described on http://drupal.org/node/778976.

I have not been able to locate an API function for doing this. Any pointers in the right direction will be appreciated.

share|improve this question
This edit has made the question more clear. Taking back my answer, because it's obviously not applicable here ;-) – develkar Dec 15 '12 at 18:46

1 Answer

up vote 3 down vote accepted

Code from standard Drupal profile is your friend: http://drupalcode.org/project/drupal.git/blob/c5d6e6334fb7a71ecf1dbc7e06a7de8ad9547b27:/profiles/standard/standard.install#l14

  // Add text formats.
  $filtered_html_format = array(
    'format' => 'filtered_html',
    'name' => 'Filtered HTML',
    'weight' => 0,
    'filters' => array(
      // URL filter.
      'filter_url' => array(
        'weight' => 0,
        'status' => 1,
      ),
      // HTML filter.
      'filter_html' => array(
        'weight' => 1,
        'status' => 1,
      ),
      // Line break filter.
      'filter_autop' => array(
        'weight' => 2,
        'status' => 1,
      ),
      // HTML corrector filter.
      'filter_htmlcorrector' => array(
        'weight' => 10,
        'status' => 1,
      ),
    ),
  );
  $filtered_html_format = (object) $filtered_html_format;
  filter_format_save($filtered_html_format);

Enable permission for system roles (code):

// Enable default permissions for system roles.
$filtered_html_permission = filter_permission_name($filtered_html_format);
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array($filtered_html_permission));
user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array($filtered_html_permission));
user_role_grant_permissions(variable_get('user_admin_role', 0), array($filtered_html_permission));
share|improve this answer
Thanks, that works! However, it shows up with "No roles may use this format" in the admin interface. Any hint on how to set the roles allowed to use it? (I'll dig through the code you link to looking for it, but I'm lazy and if someone here already knows the answer ...) It also claims it originates from the "filter" module (colum modulein the filtertable). Lying about this does not have any immediate impact, but I like my tables to be truthful :-) – Gisle Hannemyr Dec 15 '12 at 18:40
@GisleHannemyr answer was updated. – kalabro Dec 15 '12 at 18:50
Thanks! The admin role is appearently associated with rid 3 (ref table role), but unlike DRUPAL_ANONYMOUS_RID and DRUPAL_AUTHENTICATED_RID, there is no constant defined anywhere? – Gisle Hannemyr Dec 15 '12 at 19:06
@GisleHannemyr variable_get('user_admin_role', 0) – kalabro Dec 15 '12 at 20:12
Thanks again @kalabro - that was the final missing piece! The bogus filter value inserted in the modulecolumn in the table filterseems to come from the following assigment inside filter_format_save: $format->filters[$name]['module'] = $filter['module']; I think this is a bug in the core, but since the module column does not seem to be used for anything, this is isn't critical. – Gisle Hannemyr Dec 15 '12 at 21:32
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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