You are here

function openlayers_cck_content_generate in Openlayers 6

Same name and namespace in other branches
  1. 6.2 modules/openlayers_cck/openlayers_cck.module \openlayers_cck_content_generate()

Implementation of hook_content_generate().

File

modules/openlayers_cck/openlayers_cck.module, line 167
This file holds the main Drupal hook functions and private functions for the openlayers_cck module.

Code

function openlayers_cck_content_generate(&$node, $field) {
  $node_field = array();
  $types = array();
  $value = NULL;
  $multiple_count = $field['multiple'];

  // Check types
  foreach ($field['openlayers_cck_feature_types'] as $type => $available) {
    if ($available) {
      $types[] = $type;
    }
  }

  // Check multiple
  if ($field['multiple'] == 0) {

    // Only one
    $multiple_count = 1;
  }
  elseif ($field['multiple'] == 1) {

    // Unlimited
    $multiple_count = rand(3, 11);
  }

  // Create WKT
  for ($i = 0; $i < $multiple_count; $i++) {
    $type = $types[array_rand($types)];
    switch ($type) {
      case 'point':
        $value = 'POINT(' . _openlayers_cck_generate_pair() . ')';
        break;
      case 'path':

        // Make random number of points
        $line_array = array();
        for ($j = 1; $j <= rand(1, 12); $j++) {
          $line_array[] = _openlayers_cck_generate_pair();
        }
        $value = 'LINESTRING(' . implode(',', $line_array) . ')';
        break;
      case 'polygon':

        // Make random number of points
        $poly_array = array();
        for ($j = 1; $j <= rand(1, 12); $j++) {
          $poly_array[] = _openlayers_cck_generate_pair();
        }
        $value = 'POLYGON((' . implode(',', $poly_array) . '))';
        break;
    }
    $node_field[$i] = array(
      'openlayers_wkt' => $value,
    );
  }

  // Check values
  if (count($node_field) > 0) {
    return $node_field;
  }
}