You are here

function breakpoints_admin_breakpoints in Breakpoints 7

Admin form

1 string reference to 'breakpoints_admin_breakpoints'
breakpoints_menu in ./breakpoints.module
Implements hook_menu().

File

./breakpoints.admin.inc, line 11
Breakpoints - admin settings

Code

function breakpoints_admin_breakpoints($form, &$form_state, $breakpoint_group_name = '') {
  $form = array();

  // In case of an export to theme, there will be a variable exported_breakpoints
  if (isset($form_state['exported_breakpoints']) && !empty($form_state['exported_breakpoints'])) {
    $form['exported_breakpoints'] = array(
      '#title' => t('Copy/Paste the following inside your theme.info file.'),
      '#type' => 'textarea',
      '#default_value' => $form_state['exported_breakpoints'],
    );
  }

  // Global is the same as no group name
  $global = FALSE;
  if ($breakpoint_group_name == '' || $breakpoint_group_name == 'global') {
    $breakpoint_group_name = '';
    $global = TRUE;
  }
  $form_state['group_name'] = $breakpoint_group_name;
  $settings = breakpoints_settings();
  $multipliers = array();
  if (isset($settings->multipliers) && !empty($settings->multipliers)) {
    $multipliers = drupal_map_assoc(array_values($settings->multipliers));
    if (array_key_exists('1x', $multipliers)) {
      unset($multipliers['1x']);
    }
  }
  if ($global) {
    $form['info'] = array(
      '#type' => 'markup',
      '#markup' => t("You can manage all your breakpoints on this screen, if one of your themes has breakpoints defined inside the .info file they will be shown here."),
    );
    $info = array();
    $info[] = t("To create a new breakpoint, you have to enter a name and a media query (ex. (min-width: 15em)).");
    $info[] = t("All breakpoints can be enabled or disabled so they cannot be used by other modules.");
    $info[] = t("For each breakpoint you can define what multipliers have to be available (needed to support 'retina' displays).");
    $info[] = t("Breakpoints you created yourself can be deleted.");
    $info[] = t("You can group multiple breakpoints in a group by using '!add', so other modules can easily interact with them.", array(
      '!add' => l(t('Add a new group'), 'admin/config/media/breakpoints/groups/add'),
    ));
    $info[] = t("If you do not see the breakpoint group for your theme, make sure your theme is enabled and !clear_cache or click the \"Scan this theme for breakpoints\" button on the bottom of the settings page of your theme.", array(
      '!clear_cache' => l(t('clear your cache'), 'admin/config/development/performance'),
    ));
    $form['more_info'] = array(
      '#type' => 'container',
      '#theme' => 'item_list',
      '#items' => $info,
    );
  }
  else {
    $form['info'] = array(
      '#type' => 'markup',
      '#markup' => t("You can manage the breakpoints of this group here."),
    );
    $info = array();
    $info[] = t("You can change the order of the breakpoints inside this group.");
    $info[] = t("You can enable multipliers for each breakpoint, but this will also affect other groups.");
    $form['more_info'] = array(
      '#type' => 'container',
      '#theme' => 'item_list',
      '#items' => $info,
    );
  }
  $form['#attached']['css'][] = drupal_get_path('module', 'breakpoints') . '/css/breakpoints.admin.css';
  $form['breakpoints'] = array(
    '#type' => 'container',
    '#tree' => TRUE,
    '#theme' => 'breakpoints_admin_breakpoints_table',
    '#multipliers' => $multipliers,
    '#group_name' => $breakpoint_group_name,
  );
  $breakpoints = array();
  $breakpoint_group = breakpoints_breakpoint_group_load($breakpoint_group_name);
  if ($global) {
    $breakpoints = breakpoints_breakpoint_load_all();
  }
  else {
    $weight = 0;
    foreach ($breakpoint_group->breakpoints as $breakpoint_name) {
      $breakpoint = breakpoints_breakpoint_load_by_fullkey($breakpoint_name);
      if ($breakpoint && isset($breakpoint->machine_name)) {
        $breakpoint->global_weight = $breakpoint->weight;
        $breakpoint->weight = $weight++;
        $breakpoints[$breakpoint_name] = $breakpoint;
      }
    }
  }
  foreach ($breakpoints as $key => $breakpoint) {
    if ($breakpoint->source_type == BREAKPOINTS_SOURCE_TYPE_THEME) {
      $bp_group = breakpoints_breakpoint_group_load($breakpoint->source);
      if ($bp_group->overridden && variable_get('breakpoints_hide_overridden_breakpoints', 1) && $global) {
        continue;
      }
    }
    $form['breakpoints'][$key] = array(
      '#breakpoint_data' => $breakpoint,
      'name' => array(
        '#type' => 'textfield',
        '#default_value' => $breakpoint->name,
        '#disabled' => TRUE,
        '#size' => 20,
      ),
      'breakpoint' => array(
        '#type' => 'textfield',
        '#default_value' => $breakpoint->breakpoint,
        '#disabled' => $breakpoint->source_type === BREAKPOINTS_SOURCE_TYPE_THEME || !$global,
        '#size' => empty($multipliers) ? 60 : 30,
        '#maxlength' => 255,
      ),
      'weight' => array(
        '#type' => 'textfield',
        '#size' => 4,
        '#default_value' => isset($breakpoint->weight) ? $breakpoint->weight : 0,
        '#attributes' => array(
          'class' => array(
            'breakpoints-weight',
          ),
        ),
      ),
    );

    // Add multipliers checkboxes if needed.
    if (!empty($multipliers)) {
      $form['breakpoints'][$key]['multipliers'] = array(
        '#type' => 'checkboxes',
        '#default_value' => isset($breakpoint->multipliers) && is_array($breakpoint->multipliers) ? $breakpoint->multipliers : array(),
        '#options' => $multipliers,
        '#disabled' => $breakpoint->source_type === BREAKPOINTS_SOURCE_TYPE_THEME || !$global,
      );
    }

    // Add global weight if needed.
    $form['breakpoints'][$key]['global_weight'] = array(
      '#type' => 'value',
      '#value' => isset($breakpoint->global_weight) ? $breakpoint->global_weight : $breakpoint->weight,
    );
  }
  if ($global) {

    // Add empty row
    $form['breakpoints']['new'] = array(
      'name' => array(
        '#type' => 'textfield',
        '#default_value' => '',
        '#size' => 20,
        '#maxlength' => 255,
      ),
      'machine_name' => array(
        '#type' => 'machine_name',
        '#size' => '64',
        '#title' => t('Machine name'),
        '#default_value' => '',
        '#machine_name' => array(
          'exists' => 'breakpoints_breakpoint_name_exists',
          'source' => array(
            'breakpoints',
            'new',
            'name',
          ),
        ),
        '#required' => FALSE,
        '#maxlength' => 255,
      ),
      'breakpoint' => array(
        '#type' => 'textfield',
        '#default_value' => '',
        '#size' => empty($multipliers) ? 60 : 30,
        '#maxlength' => 255,
      ),
      'weight' => array(
        '#type' => 'textfield',
        '#size' => 4,
        '#default_value' => 0,
        '#attributes' => array(
          'class' => array(
            'breakpoints-weight',
          ),
        ),
      ),
    );

    // Add multipliers checkboxes if needed.
    if (!empty($multipliers)) {
      $form['breakpoints']['new']['multipliers'] = array(
        '#type' => 'checkboxes',
        '#default_value' => array(),
        '#options' => $multipliers,
      );
    }
  }

  // Buttons
  $form['buttons'] = array(
    '#type' => 'container',
  );

  // Submit button
  $form['buttons']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  if (!$global) {
    switch ($breakpoint_group->type) {
      case BREAKPOINTS_SOURCE_TYPE_THEME:
        if (!$breakpoint_group->overridden) {
          $form['buttons']['override'] = array(
            '#type' => 'submit',
            '#value' => t('Override theme breakpoints'),
            '#submit' => array(
              'breakpoints_admin_breakpoints_submit_override',
            ),
          );
          $form['buttons']['reload'] = array(
            '#type' => 'submit',
            '#value' => t('Reload theme breakpoints'),
            '#submit' => array(
              'breakpoints_admin_breakpoints_submit_reload',
            ),
          );
          $form['buttons']['duplicate'] = array(
            '#type' => 'markup',
            '#markup' => l(t('Duplicate group'), 'admin/config/media/breakpoints/groups/' . $breakpoint_group_name . '/duplicate', array(
              'query' => drupal_get_destination(),
              'attributes' => array(
                'class' => array(
                  'breakpoints-group-operations-link',
                  'breakpoints-group-operations-duplicate-link',
                ),
              ),
            )),
          );
        }
        else {
          $form['buttons']['exporttotheme'] = array(
            '#type' => 'submit',
            '#value' => t('Export breakpoints to theme'),
            '#submit' => array(
              'breakpoints_admin_breakpoints_submit_exporttotheme',
            ),
          );
          $form['buttons']['revert'] = array(
            '#type' => 'submit',
            '#value' => t('Revert theme breakpoints'),
            '#submit' => array(
              'breakpoints_admin_breakpoints_submit_revert',
            ),
          );
          $form['buttons']['editlink'] = array(
            '#type' => 'markup',
            '#markup' => l(t('Edit group breakpoints'), 'admin/config/media/breakpoints/groups/' . $breakpoint_group_name . '/edit', array(
              'query' => drupal_get_destination(),
              'attributes' => array(
                'class' => array(
                  'breakpoints-group-operations-link',
                  'breakpoints-group-operations-edit-link',
                ),
              ),
            )),
          );
          $form['buttons']['duplicate'] = array(
            '#type' => 'markup',
            '#markup' => l(t('Duplicate group'), 'admin/config/media/breakpoints/groups/' . $breakpoint_group_name . '/duplicate', array(
              'query' => drupal_get_destination(),
              'attributes' => array(
                'class' => array(
                  'breakpoints-group-operations-link',
                  'breakpoints-group-operations-duplicate-link',
                ),
              ),
            )),
          );
        }
        break;
      case BREAKPOINTS_SOURCE_TYPE_MODULE:
        $form['buttons']['exporttotheme'] = array(
          '#type' => 'submit',
          '#value' => t('Export breakpoints to theme'),
          '#submit' => array(
            'breakpoints_admin_breakpoints_submit_exporttotheme',
          ),
        );
        $form['buttons']['editlink'] = array(
          '#type' => 'markup',
          '#markup' => l(t('Edit group breakpoints'), 'admin/config/media/breakpoints/groups/' . $breakpoint_group_name . '/edit', array(
            'query' => drupal_get_destination(),
            'attributes' => array(
              'class' => array(
                'breakpoints-group-operations-link',
                'breakpoints-group-operations-edit-link',
              ),
            ),
          )),
        );
        $form['buttons']['duplicate'] = array(
          '#type' => 'markup',
          '#markup' => l(t('Duplicate group'), 'admin/config/media/breakpoints/groups/' . $breakpoint_group_name . '/duplicate', array(
            'query' => drupal_get_destination(),
            'attributes' => array(
              'class' => array(
                'breakpoints-group-operations-link',
                'breakpoints-group-operations-duplicate-link',
              ),
            ),
          )),
        );
        break;
      case BREAKPOINTS_SOURCE_TYPE_CUSTOM:
        $form['buttons']['exporttotheme'] = array(
          '#type' => 'submit',
          '#value' => t('Export breakpoints to theme'),
          '#submit' => array(
            'breakpoints_admin_breakpoints_submit_exporttotheme',
          ),
        );
        $form['buttons']['editlink'] = array(
          '#type' => 'markup',
          '#markup' => l(t('Edit group breakpoints'), 'admin/config/media/breakpoints/groups/' . $breakpoint_group_name . '/edit', array(
            'query' => drupal_get_destination(),
            'attributes' => array(
              'class' => array(
                'breakpoints-group-operations-link',
                'breakpoints-group-operations-edit-link',
              ),
            ),
          )),
        );
        $form['buttons']['duplicate'] = array(
          '#type' => 'markup',
          '#markup' => l(t('Duplicate group'), 'admin/config/media/breakpoints/groups/' . $breakpoint_group_name . '/duplicate', array(
            'query' => drupal_get_destination(),
            'attributes' => array(
              'class' => array(
                'breakpoints-group-operations-link',
                'breakpoints-group-operations-duplicate-link',
              ),
            ),
          )),
        );
        $form['buttons']['deletelink'] = array(
          '#type' => 'markup',
          '#markup' => l(t('Delete this group'), 'admin/config/media/breakpoints/groups/' . $breakpoint_group_name . '/delete', array(
            'query' => array(
              'destination' => 'admin/config/media/breakpoints/groups',
            ),
            'attributes' => array(
              'class' => array(
                'breakpoints-group-operations-link',
                'breakpoints-group-operations-delete-link',
              ),
            ),
          )),
        );
        break;
    }
  }
  return $form;
}