You are here

public function DoubleFieldWidget::__construct in Double Field 7.2

Constructor for DoubleFieldWidget.

File

tests/double_field_widget.inc, line 23
The file contains a class that assists to test Double field widget types.

Class

DoubleFieldWidget
Helper class to test Double field widget types.

Code

public function __construct($widget_type, $field_settings) {
  $this->fieldSettings = $field_settings;
  $subwidgets = explode('_&_', $widget_type);
  $this->subwidgets['first'] = $subwidgets[0];
  $this->subwidgets['second'] = $subwidgets[1];

  // Generate widget settings.
  $this->settings['inline'] = (bool) mt_rand(0, 1);
  foreach ($this->subwidgets as $index => $subwidget_type) {
    $this->settings[$index]['general'] = array(
      'required' => (bool) mt_rand(0, 1),
      'prefix' => '(' . DrupalTestCase::randomName(mt_rand(0, 126)) . ')',
      'suffix' => '(' . DrupalTestCase::randomName(mt_rand(0, 126)) . ')',
    );

    // Subwidget related settings.
    switch ($subwidget_type) {
      case 'textfield':
        $this->settings[$index][$subwidget_type]['size'] = mt_rand(1, 50);
        $this->settings[$index][$subwidget_type]['placeholder'] = DrupalTestCase::randomName(mt_rand(0, 50));
        break;
      case 'checkbox':

        // Get the value based on field settings.
        $this->settings[$index][$subwidget_type]['on_value'] = DoubleFieldField::generateValue($this->fieldSettings[$index]);
        $this->settings[$index][$subwidget_type]['off_value'] = '';
        break;
      case 'select':
        $this->settings[$index][$subwidget_type] = array();
        for ($i = 0, $cnt = mt_rand(5, 10); $i < $cnt; $i++) {

          // These values should match global field settings.
          $value = DoubleFieldField::generateValue($this->fieldSettings[$index]);
          $this->settings[$index][$subwidget_type]['allowed_values'][$value] = ucfirst($value);
        }
        break;
      case 'textarea':
        $this->settings[$index][$subwidget_type]['cols'] = mt_rand(1, 25);
        $this->settings[$index][$subwidget_type]['rows'] = mt_rand(1, 25);
        $this->settings[$index][$subwidget_type]['resizable'] = (bool) mt_rand(0, 1);
        $this->settings[$index][$subwidget_type]['placeholder'] = DrupalTestCase::randomName(mt_rand(0, 50));
        break;
      default:
        throw new Exception('Undefined subwidget type: ' . $subwidget_type);
    }

    // Generate field value based on current field and widget settings.
    for ($delta = 0; $delta < self::MAXCARDINALITY; $delta++) {
      if ($subwidget_type == 'select') {

        // Take value for select list subwidget_type from allowed values.
        $this->value[$delta][$index] = array_rand($this->settings[$index][$subwidget_type]['allowed_values']);
      }
      elseif ($subwidget_type == 'checkbox') {
        $this->value[$delta][$index] = mt_rand(0, 1) ? '' : $this->settings[$index][$subwidget_type]['on_value'];
      }
      else {
        $this->value[$delta][$index] = DoubleFieldField::generateValue($this->fieldSettings[$index]);
      }
    }
  }
}