You are here

public static function IframeItem::generateSampleValue in Iframe 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldType/IframeItem.php \Drupal\iframe\Plugin\Field\FieldType\IframeItem::generateSampleValue()

Generates placeholder field values.

Useful when populating site with placeholder content during site building or profiling.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition.

Return value

array An associative array of values.

Overrides FieldItemBase::generateSampleValue

File

src/Plugin/Field/FieldType/IframeItem.php, line 251

Class

IframeItem
Plugin implementation of the 'Iframe' field type.

Namespace

Drupal\iframe\Plugin\Field\FieldType

Code

public static function generateSampleValue(FieldDefinitionInterface $field_definition) {

  // Set of possible top-level domains.
  $tlds = [
    'com',
    'net',
    'gov',
    'org',
    'edu',
    'biz',
    'info',
  ];

  // Set random length for the domain name.
  $domain_length = mt_rand(7, 15);
  $random = new Random();
  switch ($field_definition
    ->getSetting('title')) {
    case DRUPAL_DISABLED:
      $values['title'] = '';
      break;
    case DRUPAL_REQUIRED:
      $values['title'] = $random
        ->sentences(4);
      break;
    case DRUPAL_OPTIONAL:

      // In case of optional title, randomize its generation.
      $values['title'] = mt_rand(0, 1) ? $random
        ->sentences(4) : '';
      break;
  }
  $values['url'] = 'https://www.' . $random
    ->word($domain_length) . '.' . $tlds[mt_rand(0, count($tlds) - 1)];
  return $values;
}