You are here

function safeword_field_widget_form in Safeword 8

Same name and namespace in other branches
  1. 7 safeword.module \safeword_field_widget_form()

Implements hook_field_widget_form().

safeword_machine_name uses Drupal's built-in 'Machine Readable Name' form element to display both values for editing.

File

./safeword.module, line 383
Provides a FieldAPI field type, widget, and several formatters for a combined human readable/machine name pair of values. Possible uses include automatic generation of editable pathauto segments, handy Views argument values, and impressing friends.

Code

function safeword_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // Add machine name target functionality
  $element += array(
    '#delta' => $delta,
  );

  // The $element variable is a pre-populated form element with no #type property,
  // but all the bits like #description, #title, and #required filled in as
  // appropriate. When building sub-elements for this widget, we can copy the
  // FAPI properties from the parent element.
  switch ($instance['widget']['type']) {
    case 'safeword_machine_name':
      $element['human'] = array(
        '#title' => $element['#title'],
        '#type' => 'textfield',
        '#default_value' => empty($items[$delta]['human']) ? '' : $items[$delta]['human'],
        '#maxlength' => $field['settings']['max_length'],
        '#description' => filter_xss_admin($element['#description']),
        '#required' => $element['#required'],
      );
      $safeword_source = array_merge($element['#field_parents'], array(
        $field['field_name'],
        $langcode,
        $delta,
        'human',
      ));
    case 'safeword_machine_name_only':
      if (!isset($safeword_source)) {
        $safeword_source = array(
          'title',
        );

        // Taxonomy terms use a "name" field instead of "title"
        if ($instance['entity_type'] == 'taxonomy_term') {
          $safeword_source = array(
            'name',
          );
        }
      }
      $element['machine'] = array(
        '#type' => 'machine_name',
        '#default_value' => empty($items[$delta]['machine']) ? '' : $items[$delta]['machine'],
        '#maxlength' => $field['settings']['max_length'],
        '#description' => filter_xss_admin($field['settings']['machine_description']),
        '#machine_name' => array(
          'source' => $safeword_source,
          'label' => check_plain($field['settings']['machine_label']),
          'replace_pattern' => $field['settings']['replace_pattern'],
          'replace' => $field['settings']['replace_value'],
          'field_prefix' => '',
          'field_suffix' => '',
        ),
        '#required' => $element['#required'],
        '#disabled' => !empty($items[$delta]['machine']) && empty($field['settings']['allow_machine_changes']),
      );

      // Load the JS file, provides transliteration.
      drupal_add_js(drupal_get_path('module', 'safeword') . '/safeword.js', array(
        'weight' => 1,
      ));

      // Show complete path functionality
      //   - use path auto settings to show the full URL to the user
      if ($field['settings']['show_complete_path'] && module_exists('pathauto')) {

        // Load the CSS file, styling for Pathauto path preffix and suffix.
        drupal_add_css(drupal_get_path('module', 'safeword') . '/safeword.css', array(
          'group' => CSS_DEFAULT,
          'type' => 'file',
        ));

        // Get the pattern from pathauto
        $pathauto_pattern = pathauto_pattern_load_by_entity($element['#entity_type'], $element['#bundle'], $element['#language']);

        // Find out what the token name will be
        $token = '[' . $element['#entity_type'] . ':' . $field['field_name'] . ']';

        // Edge case: if "Machine name from title", and there is no pathauto
        //  token containing field name but there is an entity:title token,
        //  replace the title instead
        if ($field['type'] == 'safeword_title') {
          $title_field_token = '[' . $element['#entity_type'] . ':title]';
          if (strpos($pathauto_pattern, $token) === FALSE && strpos($pathauto_pattern, $title_field_token) !== FALSE) {
            $token = $title_field_token;
          }
        }

        // Explode the path based on the token
        $exploded_path = explode($token, $pathauto_pattern);
        if (count($exploded_path) > 1) {
          foreach ($exploded_path as $path_key => $path_part) {

            //$exploded_path[$path_key] = '<span class="safeword-path-un-editable">' . $path_part . '</span>';
            $exploded_path[$path_key] = theme('safeword_un_editable_path_parts', array(
              'path' => check_plain($path_part),
            ));
          }
          $element['machine']['#machine_name']['field_prefix'] = array_shift($exploded_path);

          // Implode the remaining path - if the token appears more than once
          // then only replace the first version.
          $element['machine']['#machine_name']['field_suffix'] = implode($token, $exploded_path);
        }
      }

      // Only add the code for the uniqueness check if the field requires it.
      // No need to trigger the extra checks.
      if ($field['settings']['unique']) {
        $element['machine']['#machine_name']['exists'] = 'safeword_value_collides';
        $element['machine']['#exists_params'] = array(
          'field_name' => $field['field_name'],
          'entity_type' => $instance['entity_type'],
          'bundle' => $instance['bundle'],
        );
      }
      else {
        $element['machine']['#machine_name']['exists'] = 'safeword_value_not_unique';
      }
  }
  return $element;
}