You are here

function simplenews_issue_newsletter_field_add in Simplenews 7.2

Add field for newsletter

Parameters

$type A node type object.:

Related topics

2 calls to simplenews_issue_newsletter_field_add()
simplenews_issue_fields_add in ./simplenews.module
Add fields for newsletter, status and sent count to a note type.
simplenews_update_7200 in ./simplenews.install
Replace {simplenews_newsletter}.

File

./simplenews.module, line 494
Simplenews node handling, sent email, newsletter block and general hooks

Code

function simplenews_issue_newsletter_field_add($type) {
  $field_name = variable_get('simplenews_newsletter_field', 'simplenews_newsletter');
  $field = field_info_field($field_name);
  $instance = field_info_instance('node', $field_name, $type->type);
  if (empty($field)) {
    $field = array(
      'field_name' => $field_name,
      'type' => 'entityreference',
      'cardinality' => 1,
      'entity_types' => array(
        'node',
      ),
      'translatable' => FALSE,
      'settings' => array(
        'target_type' => 'simplenews_newsletter',
        'handler' => 'base',
        'handler_submit' => 'Change handler',
        'handler_settings' => array(
          'sort' => array(
            'type' => 'property',
            'property' => 'weight',
            'direction' => 'ASC',
          ),
          'behaviors' => array(
            'views-select-list' => array(
              'status' => 1,
            ),
          ),
        ),
      ),
    );
    $field = field_create_field($field);
  }
  if (empty($instance)) {
    $newsletter = simplenews_newsletter_get_all();
    $default_value = array();
    if ($newsletter) {
      $newsletter = reset($newsletter);
      $default_value = array(
        array(
          'target_id' => $newsletter->newsletter_id,
        ),
      );
    }
    $instance = array(
      'label' => t('Newsletter'),
      'field_name' => $field_name,
      'bundle' => $type->type,
      'entity_type' => 'node',
      'required' => TRUE,
      'default_value' => $default_value,
      'widget' => array(
        'type' => 'options_select',
      ),
      'display' => array(
        'default' => array(
          'label' => 'hidden',
          'type' => 'entityreference_label',
        ),
        'teaser' => array(
          'label' => 'hidden',
          'type' => 'hidden',
        ),
        'email_plain' => array(
          'label' => 'hidden',
          'type' => 'hidden',
        ),
        'email_html' => array(
          'label' => 'hidden',
          'type' => 'hidden',
        ),
        'email_textalt' => array(
          'label' => 'hidden',
          'type' => 'hidden',
        ),
      ),
    );
    field_create_instance($instance);
  }
}