0

I would like add a new setting for field which would allow to choose a icon for a field same as cardinality or any other settings. /admin/structure/types/manage/[content_Type]/fields/field_[some_field_name]

I don't want it per instance but for a field type e.g. phone number could have phone icon. I also don't want to play with hard coded templates and I'd like to use Icon api module's icon selector from ui.

There's a lot of hooks available and so far I've managed to get this icon selector to settings form (not sure how I made it) and also to save the value. However, I cannot access icon data from forms such as views exposed forms which include these fields widgets with assigned icon since icon data is not available in field settings data or similar. There's only maxlength, size etc. "settings" available but not this icon setting.

function mymodule_field_info_alter(&$info) {
  // Add a setting to all field types.
  foreach ($info as $field_type => $field_type_info) {
    $info[$field_type]['settings'] += array(
      'icon' => array(
        'icon_field' => array(
          'label' => t('Icon'),
          'description' => t('Store a bundle and icon in the database to assemble an icon field.'),
          'settings' => array(
            'bundle' => NULL,
            'icon' => NULL,
            'wrapper' => NULL,
            'wrapper_class' => NULL,
          ),
          'instance_settings' => array(
            'bundle' => NULL,
            'icon' => NULL,
          ),
          'default_widget' => 'icon_field',
          'default_formatter' => 'icon_field_default',
        ),
      )
    );
    $info[$field_type]['instance_settings'] += array('icon');
  }

}

function mymodule_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {


  $form['field']['settings']['icon'] = array(
    '#type' => 'icon_selector',
    '#title' => t('Icon'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#default_bundle' => (isset( $form['#field']['settings']['icon']) && $form['#field']['settings']['icon']['bundle'] !== '') ? $form['#field']['settings']['icon']['bundle'] : '',
    '#default_icon' =>  (isset( $form['#field']['settings']['icon']) && $form['#field']['settings']['icon']['icon'] !== '') ? $form['#field']['settings']['icon']['icon'] : '',
  );
}

function mymodule_field_settings_form($field, $instance, $has_data) {
  $settings = $field['settings'];

  $form['icon'] = array(
    '#type' => 'icon_selector',
    '#title' => t('Icon'),
    '#required' => FALSE,

  );
  return $form;
}

Your Answer

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

Browse other questions tagged or ask your own question.