You are here

class boxes_i18n in Boxes translation 7

Same name and namespace in other branches
  1. 6 plugins/boxes/boxes_i18n.inc \boxes_i18n

Simple translatable custom text box.

Hierarchy

Expanded class hierarchy of boxes_i18n

1 string reference to 'boxes_i18n'
i18n_boxes_boxes_plugins in ./i18n_boxes.module
Implements hook_boxes_plugins().

File

plugins/boxes/boxes_i18n.inc, line 6

View source
class boxes_i18n extends boxes_simple {

  /**
   * Implements boxes_content::options_defaults().
   */
  public function options_defaults() {
    $defaults = parent::options_defaults();
    return $defaults + array(
      'i18n_boxes' => array(
        'language' => I18N_BOXES_LOCALIZE,
      ),
    );
  }

  /**
   * Implements boxes_content::options_form().
   */
  public function options_form(&$form_state) {
    $form = parent::options_form($form_state);
    $form['i18n_boxes'] = array(
      '#type' => 'fieldset',
      '#title' => t('Multilingual settings'),
      '#collapsible' => TRUE,
      '#weight' => 0,
      '#tree' => TRUE,
    );
    $options = array(
      '' => t('All languages'),
    );
    $options[I18N_BOXES_LOCALIZE] = t('All languages (Translatable)');
    $options += locale_language_list('name');
    $form['i18n_boxes']['language'] = array(
      '#type' => 'radios',
      '#title' => t('Language'),
      '#default_value' => $this->options['i18n_boxes']['language'],
      '#options' => $options,
    );
    return $form;
  }

  /**
   * Save a box.
   */
  public function save() {
    parent::save();

    // Make the box strings available to be translated.
    $this
      ->locale_refresh();
  }

  /**
   * Refresh the translatable strings.
   */
  public function locale_refresh() {
    if ($this->options['i18n_boxes']['language'] == I18N_BOXES_LOCALIZE) {

      // Update the box's body field; if empty, remove the translation.
      if (!empty($this->options['body']['value'])) {
        i18n_string_update(array(
          'boxes',
          $this->plugin_key,
          $this->delta,
          'body',
        ), $this->options['body']['value'], array(
          'format' => $this->options['body']['format'],
        ));
      }
      else {
        i18n_string_remove(array(
          'boxes',
          $this->plugin_key,
          $this->delta,
          'body',
        ));
      }

      // Update the box's title field; if empty, remove the translation.
      if (!empty($this->title)) {
        i18n_string_update(array(
          'boxes',
          $this->plugin_key,
          $this->delta,
          'title',
        ), $this->title);
      }
      else {
        i18n_string_remove(array(
          'boxes',
          $this->plugin_key,
          $this->delta,
          'title',
        ));
      }
    }
    return TRUE;
  }

  /**
   * Implements boxes_content::render().
   */
  public function render() {
    global $language;

    // Get the filtered rendered data from boxes_simple.
    $block = parent::render();

    // Handle any multilingual settings.
    if (!empty($this->options['i18n_boxes']['language'])) {
      if ($this->options['i18n_boxes']['language'] == I18N_BOXES_LOCALIZE) {

        // This box is available to translate.
        $block['content'] = i18n_string_text(array(
          'boxes',
          $this->plugin_key,
          $this->delta,
          'body',
        ), $block['content'], array(
          'format' => $this->options['body']['format'],
        ));

        // Also translate the title.
        $block['subject'] = i18n_string_plain(array(
          'boxes',
          $this->plugin_key,
          $this->delta,
          'title',
        ), $block['subject']);
        $block['title'] = $block['subject'];
      }
      elseif ($this->options['i18n_boxes']['language'] != $language->language) {

        // This box does not share the language of the user, so do not display.
        $block = NULL;
      }
    }
    return $block;
  }
  public function cache_setting() {
    return DRUPAL_CACHE_CUSTOM;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
boxes_box::$boxes static property
boxes_box::$delta public property
boxes_box::$description public property
boxes_box::$export_type public property
boxes_box::$new public property
boxes_box::$options public property
boxes_box::$plugin_key public property
boxes_box::$title public property
boxes_box::delete public function Delete a box.
boxes_box::factory public static function Instantiate, populate and return a box object.
boxes_box::load public static function Load existing box by its unique identifier $delta.
boxes_box::reset public static function Reset the boxes cache.
boxes_box::use_multistep_create public function Declare if the box should use a multistep form for the create form.
boxes_box::__construct protected function Create a new box.
boxes_i18n::cache_setting public function Returns the block cache settings for this box. Subclasses can override this to perform more intricate operations around deciding the cache settings of the specific box instance. Overrides boxes_box::cache_setting
boxes_i18n::locale_refresh public function Refresh the translatable strings.
boxes_i18n::options_defaults public function Implements boxes_content::options_defaults(). Overrides boxes_simple::options_defaults
boxes_i18n::options_form public function Implements boxes_content::options_form(). Overrides boxes_simple::options_form
boxes_i18n::render public function Implements boxes_content::render(). Overrides boxes_simple::render
boxes_i18n::save public function Save a box. Overrides boxes_box::save