/**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     $date_format = DateFormat::load('html_date')->getPattern();
     $time_format = DateFormat::load('html_time')->getPattern();
     $default_value = isset($items[$delta]->value) ? DrupalDateTime::createFromTimestamp($items[$delta]->value) : '';
     $element['value'] = $element + array('#type' => 'datetime', '#default_value' => $default_value, '#date_year_range' => '1902:2037');
     $element['value']['#description'] = $this->t('Format: %format. Leave blank to use the time of form submission.', array('%format' => Datetime::formatExample($date_format . ' ' . $time_format)));
     return $element;
 }
Example #2
0
 /**
  * Tests the display of dates and time when user-configurable time zones are set.
  */
 function testUserTimeZone()
 {
     // Setup date/time settings for Los Angeles time.
     $this->config('system.date')->set('timezone.user.configurable', 1)->set('timezone.default', 'America/Los_Angeles')->save();
     DateFormat::load('medium')->setPattern('Y-m-d H:i T')->save();
     // Create a user account and login.
     $web_user = $this->drupalCreateUser();
     $this->drupalLogin($web_user);
     // Create some nodes with different authored-on dates.
     // Two dates in PST (winter time):
     $date1 = '2007-03-09 21:00:00 -0800';
     $date2 = '2007-03-11 01:00:00 -0800';
     // One date in PDT (summer time):
     $date3 = '2007-03-20 21:00:00 -0700';
     $this->drupalCreateContentType(array('type' => 'article'));
     $node1 = $this->drupalCreateNode(array('created' => strtotime($date1), 'type' => 'article'));
     $node2 = $this->drupalCreateNode(array('created' => strtotime($date2), 'type' => 'article'));
     $node3 = $this->drupalCreateNode(array('created' => strtotime($date3), 'type' => 'article'));
     // Confirm date format and time zone.
     $this->drupalGet('node/' . $node1->id());
     $this->assertText('2007-03-09 21:00 PST', 'Date should be PST.');
     $this->drupalGet('node/' . $node2->id());
     $this->assertText('2007-03-11 01:00 PST', 'Date should be PST.');
     $this->drupalGet('node/' . $node3->id());
     $this->assertText('2007-03-20 21:00 PDT', 'Date should be PDT.');
     // Change user time zone to Santiago time.
     $edit = array();
     $edit['mail'] = $web_user->getEmail();
     $edit['timezone'] = 'America/Santiago';
     $this->drupalPostForm("user/" . $web_user->id() . "/edit", $edit, t('Save'));
     $this->assertText(t('The changes have been saved.'), 'Time zone changed to Santiago time.');
     // Confirm date format and time zone.
     $this->drupalGet('node/' . $node1->id());
     $this->assertText('2007-03-10 02:00 CLST', 'Date should be Chile summer time; five hours ahead of PST.');
     $this->drupalGet('node/' . $node2->id());
     $this->assertText('2007-03-11 05:00 CLT', 'Date should be Chile time; four hours ahead of PST');
     $this->drupalGet('node/' . $node3->id());
     $this->assertText('2007-03-21 00:00 CLT', 'Date should be Chile time; three hours ahead of PDT.');
     // Ensure that anonymous users also use the default timezone.
     $this->drupalLogout();
     $this->drupalGet('node/' . $node1->id());
     $this->assertText('2007-03-09 21:00 PST', 'Date should be PST.');
     $this->drupalGet('node/' . $node2->id());
     $this->assertText('2007-03-11 01:00 PST', 'Date should be PST.');
     $this->drupalGet('node/' . $node3->id());
     $this->assertText('2007-03-20 21:00 PDT', 'Date should be PDT.');
     // Format a date without accessing the current user at all and
     // ensure that it uses the default timezone.
     $this->drupalGet('/system-test/date');
     $this->assertText('2016-01-13 08:29 PST', 'Date should be PST.');
 }
 /**
  * Tests the Drupal 6 date formats to Drupal 8 migration.
  */
 public function testDateFormats()
 {
     $short_date_format = DateFormat::load('short');
     $this->assertIdentical('\\S\\H\\O\\R\\T m/d/Y - H:i', $short_date_format->getPattern());
     $medium_date_format = DateFormat::load('medium');
     $this->assertIdentical('\\M\\E\\D\\I\\U\\M D, m/d/Y - H:i', $medium_date_format->getPattern());
     $long_date_format = DateFormat::load('long');
     $this->assertIdentical('\\L\\O\\N\\G l, F j, Y - H:i', $long_date_format->getPattern());
     // Test that we can re-import using the EntityDateFormat destination.
     Database::getConnection('default', 'migrate')->update('variable')->fields(array('value' => serialize('\\S\\H\\O\\R\\T d/m/Y - H:i')))->condition('name', 'date_format_short')->execute();
     $migration = $this->getMigration('d6_date_formats');
     \Drupal::database()->truncate($migration->getIdMap()->mapTableName())->execute();
     $this->executeMigration($migration);
     $short_date_format = DateFormat::load('short');
     $this->assertIdentical('\\S\\H\\O\\R\\T d/m/Y - H:i', $short_date_format->getPattern());
 }
 /**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     $element = parent::formElement($items, $delta, $element, $form, $form_state);
     // Remove 'Leave blank to use the time of form submission' which is in the
     // #description inherited from TimestampDatetimeWidget. The text here is not
     // used because it is entirely replaced in scheduler_form_node_form_alter()
     // However the widget is generic and may be used elsewhere in future.
     $date_format = DateFormat::load('html_date')->getPattern();
     $time_format = DateFormat::load('html_time')->getPattern();
     $element['value']['#description'] = $this->t('Format: %format. Leave blank for no date.', array('%format' => Datetime::formatExample($date_format . ' ' . $time_format)));
     // Set the callback function to allow interception of the submitted user
     // input and add the default time if needed. It is too late to try this in
     // function massageFormValues as the validation has already been done.
     $element['value']['#value_callback'] = array($this, 'valueCallback');
     return $element;
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $config = $this->config('webform.settings');
     $form['#tree'] = TRUE;
     $form['components'] = array('#type' => 'details', '#title' => t('Available components'), '#description' => t('These are the available field types for your installation of Webform. You may disable any of these components by unchecking its corresponding box. Only checked components will be available in existing or new webforms.'), '#open' => TRUE, '#theme' => 'webform_admin_settings_components_table');
     // Add each component to the form.
     $manager = \Drupal::service('plugin.manager.webform.component');
     $component_types = $manager->getDefinitions();
     foreach ($component_types as $key => $component_type) {
         $component = $manager->createInstance($component_type['id']);
         $form['components'][$key] = array('#type' => 'checkbox', '#title' => $component->getLabel(), '#description' => $component->getDescription(), '#return_value' => 1, '#default_value' => 1);
     }
     $form['email'] = array('#type' => 'details', '#title' => t('Default e-mail values'), '#open' => TRUE);
     $form['email']['default_from_address'] = array('#type' => 'textfield', '#title' => t('From address'), '#default_value' => $config->get('email.default_from_address'), '#description' => t('The default sender address for emailed webform results; often the e-mail address of the maintainer of your forms.'));
     $form['email']['default_from_name'] = array('#type' => 'textfield', '#title' => t('From name'), '#default_value' => $config->get('email.default_from_name'), '#description' => t('The default sender name which is used along with the default from address.'));
     $form['email']['default_subject'] = array('#type' => 'textfield', '#title' => t('Default subject'), '#default_value' => $config->get('email.default_subject'), '#description' => t('The default subject line of any e-mailed results.'));
     $form['email']['replyto'] = array('#type' => 'checkbox', '#title' => t('Use Reply-To header'), '#default_value' => $config->get('email.replyto'), '#description' => t('Sends all e-mail from the domain of the default address above and sets the "Reply-To" header to the actual sender. Helps prevent e-mail from being flagged as spam.'));
     $form['email']['html_capable'] = array('#type' => 'checkbox', '#title' => t('HTML mail system'), '#default_value' => $config->get('email.html_capable'), '#description' => t('Whether the mail system configured for webform is capable of sending mail in HTML format.'));
     $form['email']['default_format'] = array('#type' => 'radios', '#title' => t('Format'), '#options' => array(0 => t('Plain text'), 1 => t('HTML')), '#default_value' => $config->get('email.default_format'), '#description' => t('The default format for new e-mail settings. Webform e-mail options take precedence over the settings for system-wide e-mails configured in MIME mail.'), '#states' => array('visible' => array(':input[name="email[html_capable]"]' => array('checked' => TRUE))));
     $form['email']['format_override'] = array('#type' => 'radios', '#title' => t('Format override'), '#options' => array(0 => t('Per-webform configuration of e-mail format'), 1 => t('Send all e-mails in the default format')), '#default_value' => $config->get('email.format_override'), '#description' => t('Force all webform e-mails to be sent in the default format.'), '#states' => array('visible' => array(':input[name="email[html_capable]"]' => array('checked' => TRUE))));
     $form['progressbar'] = array('#type' => 'details', '#title' => t('Progress bar'), '#open' => TRUE);
     $form['progressbar']['style'] = array('#type' => 'checkboxes', '#title' => t('Progress bar style'), '#options' => array('progressbar_bar' => t('Show progress bar'), 'progressbar_page_number' => t('Show page number as number of completed (i.e. Page 1 of 10)'), 'progressbar_percent' => t('Show percentage completed (i.e. 10%)'), 'progressbar_pagebreak_labels' => t('Show page labels from page break components'), 'progressbar_include_confirmation' => t('Include confirmation page in progress bar')), '#default_value' => $config->get('progressbar.style'), '#description' => t('Choose how the progress bar should be displayed for multi-page forms.'));
     $form['progressbar']['label_first'] = array('#type' => 'textfield', '#title' => t('First page label'), '#default_value' => $config->get('progressbar.label_first'), '#maxlength' => 255);
     $form['progressbar']['label_confirmation'] = array('#type' => 'textfield', '#title' => t('Confirmation page label'), '#default_value' => $config->get('progressbar.label_confirmation'), '#maxlength' => 255);
     $form['advanced'] = array('#type' => 'details', '#title' => t('Advanced options'), '#open' => FALSE);
     $form['advanced']['tracking_mode'] = array('#type' => 'radios', '#title' => t('Track anonymous users by:'), '#options' => array('cookie' => t('Cookie only (least strict)'), 'ip_address' => t('IP address only'), 'strict' => t('Both cookie and IP address (most strict)')), '#default_value' => $config->get('advanced.tracking_mode'), '#description' => t('<a href="http://www.wikipedia.org/wiki/HTTP_cookie">Cookies</a> can be used to help prevent the same user from repeatedly submitting a webform. Limiting by IP address is more effective against repeated submissions, but may result in unintentional blocking of users sharing the same address. Confidential submissions are tracked by cookie only. Logged-in users are always tracked by their user ID and are not affected by this option.'));
     $form['advanced']['email_address_format'] = array('#type' => 'radios', '#title' => t('E-mail address format'), '#options' => array('long' => t('Long format: "Example Name" &lt;name@example.com&gt;'), 'short' => t('Short format: name@example.com')), '#default_value' => $config->get('advanced.email_address_format'), '#description' => t('Most servers support the "long" format which will allow for more friendly From addresses in e-mails sent. However many Windows-based servers are unable to send in the long format. Change this option if experiencing problems sending e-mails with Webform.'));
     $form['advanced']['email_address_individual'] = array('#type' => 'radios', '#title' => t('E-mailing multiple recipients'), '#options' => array(0 => t('Send a single e-mail to all recipients'), 1 => t('Send individual e-mails to each recipient')), '#default_value' => $config->get('advanced.email_address_individual'), '#description' => t('Individual e-mails increases privacy by not revealing the addresses of other recipients. A single e-mail to all recipients lets them use "Reply All" to communicate.'));
     $date_types = DateFormat::loadMultiple();
     $date_formatter = \Drupal::service('date.formatter');
     $date_format_options = array();
     foreach ($date_types as $machine_name => $format) {
         $date_format_options[$machine_name] = t('@name - @sample', array('@name' => $format->get('label'), '@sample' => $date_formatter->format(REQUEST_TIME, $machine_name)));
     }
     $form['advanced']['date_type'] = array('#type' => 'select', '#title' => t('Date format'), '#options' => $date_format_options, '#default_value' => $config->get('advanced.date_type'), '#description' => t('Choose the format for the display of date components. Only the date portion of the format is used. Reporting and export use the short format.'));
     module_load_include('inc', 'webform', 'includes/webform.export');
     $form['advanced']['export_format'] = array('#type' => 'radios', '#title' => t('Default export format'), '#options' => webform_export_list(), '#default_value' => $config->get('advanced.export_format'));
     $form['advanced']['csv_delimiter'] = array('#type' => 'select', '#title' => t('Default export delimiter'), '#description' => t('This is the delimiter used in the CSV/TSV file when downloading Webform results. Using tabs in the export is the most reliable method for preserving non-latin characters. You may want to change this to another character depending on the program with which you anticipate importing results.'), '#default_value' => $config->get('advanced.csv_delimiter'), '#options' => array(',' => t('Comma (,)'), '\\t' => t('Tab (\\t)'), ';' => t('Semicolon (;)'), ':' => t('Colon (:)'), '|' => t('Pipe (|)'), '.' => t('Period (.)'), ' ' => t('Space ( )')));
     $form['advanced']['export_wordwrap'] = array('#type' => 'radios', '#title' => t('Export word-wrap'), '#options' => array('0' => t('Only text containing return characters'), '1' => t('All text')), '#default_value' => $config->get('advanced.export_wordwrap'), '#description' => t('Some export formats, such as Microsoft Excel, support word-wrapped text cells.'));
     $form['advanced']['submission_access_control'] = array('#type' => 'radios', '#title' => t('Submission access control'), '#options' => array('1' => t('Select the user roles that may submit each individual webform'), '0' => t('Disable Webform submission access control')), '#default_value' => $config->get('advanced.submission_access_control'), '#description' => t('By default, the configuration form for each webform allows the administrator to choose which roles may submit the form. You may want to allow users to always submit the form if you are using a separate node access module to control access to webform nodes themselves.'));
     $form['advanced']['token_access'] = array('#type' => 'radios', '#title' => t('Token access'), '#options' => array('1' => t('Allow tokens to be used in Webforms.'), '0' => t('Disable tokens in Webforms')), '#default_value' => $config->get('advanced.token_access'), '#description' => t('Tokens can be used to reveal sensitive information. Allow tokens if Webform creators are trusted.'));
     $form['advanced']['email_select_max'] = array('#type' => 'textfield', '#title' => t("Select email mapping limit"), '#default_value' => $config->get('advanced.email_select_max'), '#description' => t('When mapping emails addresses to a select component, limit the choice to components with less than the amount of options indicated. This is to avoid flooding the email settings form.'));
     return parent::buildForm($form, $form_state);
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $config = $this->config('backup_db.settings');
     // general
     $form['path'] = array('#type' => 'textfield', '#title' => $this->t('Path'), '#description' => $this->t('The path database backups are saved to, should be a URI.'), '#default_value' => $config->get('path'));
     $date_types = DateFormat::loadMultiple();
     $date_formatter = \Drupal::service('date.formatter');
     $date_format_options = array();
     foreach ($date_types as $machine_name => $format) {
         $date_format_options[$machine_name] = t('@name - @sample', array('@name' => $format->label(), '@sample' => $date_formatter->format(REQUEST_TIME, $machine_name)));
     }
     $form['date'] = array('#type' => 'select', '#title' => $this->t('Date format'), '#options' => $date_format_options, '#description' => $this->t('Creates sub folders inside path with date format name.'), '#default_value' => $config->get('date'));
     // mysqldump settings
     $form['compress'] = array('#type' => 'select', '#title' => $this->t('Compress'), '#options' => array('None' => $this->t('None'), 'Gzip' => 'Gzip', 'Bzip2' => 'Bzip2'), '#description' => $this->t('Compress the database export.'), '#default_value' => $config->get('settings.compress'));
     $form['no_data'] = array('#type' => 'checkbox', '#title' => $this->t('No data'), '#description' => $this->t('Do not write any table row information.'), '#default_value' => $config->get('settings.no_data'));
     $form['add_drop_table'] = array('#type' => 'checkbox', '#title' => $this->t('Add drop table'), '#description' => $this->t('Write a DROP TABLE statement before each CREATE TABLE statement.'), '#default_value' => $config->get('settings.add_drop_table'));
     $form['single_transaction'] = array('#type' => 'checkbox', '#title' => $this->t('Single transaction'), '#description' => $this->t('Sets the transaction isolation mode to REPEATABLE READ and sends a START TRANSACTION SQL statement to the server before dumping data.'), '#default_value' => $config->get('settings.single_transaction'));
     $form['lock_tables'] = array('#type' => 'checkbox', '#title' => $this->t('Lock tables'), '#description' => $this->t('For each dumped database, lock all tables to be dumped before dumping them.'), '#default_value' => $config->get('settings.lock_tables'));
     $form['add_locks'] = array('#type' => 'checkbox', '#title' => $this->t('Add locks'), '#description' => $this->t('Surround each table dump with LOCK TABLES and UNLOCK TABLES statements. This results in faster inserts when the dump file is reloaded.'), '#default_value' => $config->get('settings.add_locks'));
     $form['extended_insert'] = array('#type' => 'checkbox', '#title' => $this->t('Extended insert'), '#description' => $this->t('Write INSERT statements using multiple-row syntax that includes several VALUES lists. This results in a smaller dump file and speeds up inserts when the file is reloaded.'), '#default_value' => $config->get('settings.extended_insert'));
     $form['complete_insert'] = array('#type' => 'checkbox', '#title' => $this->t('Complete insert'), '#description' => $this->t('Use complete INSERT statements that include column names.'), '#default_value' => $config->get('settings.complete_insert'));
     $form['disable_keys'] = array('#type' => 'checkbox', '#title' => $this->t('Disable keys'), '#description' => $this->t('Makes loading the dump file faster because the indexes are created after all rows are inserted.'), '#default_value' => $config->get('settings.disable_keys'));
     $form['no_create_info'] = array('#type' => 'checkbox', '#title' => $this->t('No create info'), '#description' => $this->t('Do not write CREATE TABLE statements that create each dumped table.'), '#default_value' => $config->get('settings.no_create_info'));
     $form['skip_triggers'] = array('#type' => 'checkbox', '#title' => $this->t('Skip triggers'), '#description' => $this->t('Include triggers for each dumped table in the output.'), '#default_value' => $config->get('settings.skip_triggers'));
     $form['add_drop_trigger'] = array('#type' => 'checkbox', '#title' => $this->t('Add drop trigger'), '#description' => $this->t('Write a DROP TRIGGER statement before each CREATE TRIGGER statement.'), '#default_value' => $config->get('settings.add_drop_trigger'));
     $form['routines'] = array('#type' => 'checkbox', '#title' => $this->t('Routines'), '#description' => $this->t('Include stored routines (procedures and functions) for the dumped databases in the output. '), '#default_value' => $config->get('settings.routines'));
     $form['hex_blob'] = array('#type' => 'checkbox', '#title' => $this->t('Hex blob'), '#description' => $this->t('Dump binary columns using hexadecimal notation.'), '#default_value' => $config->get('settings.hex_blob'));
     $form['databases'] = array('#type' => 'checkbox', '#title' => $this->t('Databases'), '#description' => $this->t('Treat all name arguments as database names.'), '#default_value' => $config->get('settings.databases'));
     $form['add_drop_database'] = array('#type' => 'checkbox', '#title' => $this->t('Add drop database'), '#description' => $this->t('Write a DROP DATABASE statement before each CREATE DATABASE statement.'), '#default_value' => $config->get('settings.add_drop_database'));
     $form['skip_tz_utc'] = array('#type' => 'checkbox', '#title' => $this->t('Skip TZ UTC'), '#description' => $this->t('This option enables TIMESTAMP columns to be dumped and reloaded between servers in different time zones.'), '#default_value' => $config->get('settings.skip_tz_utc'));
     $form['no_autocommit'] = array('#type' => 'checkbox', '#title' => $this->t('No autocommit'), '#description' => $this->t('Please see http://dev.mysql.com/doc/refman/5.7/en/commit.html'), '#default_value' => $config->get('settings.no_autocommit'));
     $form['skip_comments'] = array('#type' => 'checkbox', '#title' => $this->t('Skip comments'), '#description' => $this->t('Write additional information in the dump file such as program version, server version, and host.'), '#default_value' => $config->get('settings.skip_comments'));
     $form['skip_dump_date'] = array('#type' => 'checkbox', '#title' => $this->t('Skip dump date'), '#description' => $this->t('Produces a date comment at the end of the dump file.'), '#default_value' => $config->get('settings.skip_dump_date'));
     $form['default_character_set'] = array('#type' => 'textfield', '#title' => $this->t('Default character set'), '#description' => $this->t('Please see http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html'), '#default_value' => $config->get('settings.default_character_set'));
     $form['where'] = array('#type' => 'textfield', '#title' => $this->t('Where'), '#description' => $this->t('Dump only rows selected by the given WHERE condition.'), '#default_value' => $config->get('settings.where'));
     return parent::buildForm($form, $form_state);
 }
Example #7
0
 /**
  * Test date format configuration.
  */
 function testDateFormatConfiguration()
 {
     // Confirm 'no custom date formats available' message appears.
     $this->drupalGet('admin/config/regional/date-time');
     // Add custom date format.
     $this->clickLink(t('Add format'));
     $date_format_id = strtolower($this->randomMachineName(8));
     $name = ucwords($date_format_id);
     $date_format = 'd.m.Y - H:i';
     $edit = array('id' => $date_format_id, 'label' => $name, 'date_format_pattern' => $date_format);
     $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
     $this->assertUrl(\Drupal::url('entity.date_format.collection', [], ['absolute' => TRUE]), [], 'Correct page redirection.');
     $this->assertText(t('Custom date format added.'), 'Date format added confirmation message appears.');
     $this->assertText($name, 'Custom date format appears in the date format list.');
     $this->assertText(t('Delete'), 'Delete link for custom date format appears.');
     // Edit the custom date format and re-save without editing the format.
     $this->drupalGet('admin/config/regional/date-time');
     $this->clickLink(t('Edit'));
     $this->drupalPostForm(NULL, NULL, t('Save format'));
     $this->assertUrl('admin/config/regional/date-time', array('absolute' => TRUE), 'Correct page redirection.');
     $this->assertText(t('Custom date format updated.'), 'Custom date format successfully updated.');
     // Edit custom date format.
     $this->drupalGet('admin/config/regional/date-time');
     $this->clickLink(t('Edit'));
     $edit = array('date_format_pattern' => 'Y m');
     $this->drupalPostForm($this->getUrl(), $edit, t('Save format'));
     $this->assertUrl(\Drupal::url('entity.date_format.collection', [], ['absolute' => TRUE]), [], 'Correct page redirection.');
     $this->assertText(t('Custom date format updated.'), 'Custom date format successfully updated.');
     // Delete custom date format.
     $this->clickLink(t('Delete'));
     $this->drupalPostForm('admin/config/regional/date-time/formats/manage/' . $date_format_id . '/delete', array(), t('Delete'));
     $this->assertUrl(\Drupal::url('entity.date_format.collection', [], ['absolute' => TRUE]), [], 'Correct page redirection.');
     $this->assertRaw(t('The date format %format has been deleted.', array('%format' => $name)), 'Custom date format removed.');
     // Make sure the date does not exist in config.
     $date_format = entity_load('date_format', $date_format_id);
     $this->assertFalse($date_format);
     // Add a new date format with an existing format.
     $date_format_id = strtolower($this->randomMachineName(8));
     $name = ucwords($date_format_id);
     $date_format = 'Y';
     $edit = array('id' => $date_format_id, 'label' => $name, 'date_format_pattern' => $date_format);
     $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
     $this->assertUrl(\Drupal::url('entity.date_format.collection', [], ['absolute' => TRUE]), [], 'Correct page redirection.');
     $this->assertText(t('Custom date format added.'), 'Date format added confirmation message appears.');
     $this->assertText($name, 'Custom date format appears in the date format list.');
     $this->assertText(t('Delete'), 'Delete link for custom date format appears.');
     $date_format = DateFormat::create(array('id' => 'xss_short', 'label' => 'XSS format', 'pattern' => '\\<\\s\\c\\r\\i\\p\\t\\>\\a\\l\\e\\r\\t\\(\'\\X\\S\\S\'\\)\\;\\<\\/\\s\\c\\r\\i\\p\\t\\>'));
     $date_format->save();
     $this->drupalGet(Url::fromRoute('entity.date_format.collection'));
     $this->assertEscaped("<script>alert('XSS');</script>", 'The date format was properly escaped');
     // Add a new date format with HTML in it.
     $date_format_id = strtolower($this->randomMachineName(8));
     $name = ucwords($date_format_id);
     $date_format = '& \\<\\e\\m\\>Y\\<\\/\\e\\m\\>';
     $edit = array('id' => $date_format_id, 'label' => $name, 'date_format_pattern' => $date_format);
     $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
     $this->assertUrl(\Drupal::url('entity.date_format.collection', [], ['absolute' => TRUE]), [], 'Correct page redirection.');
     $this->assertText(t('Custom date format added.'), 'Date format added confirmation message appears.');
     $this->assertText($name, 'Custom date format appears in the date format list.');
     $this->assertEscaped('<em>' . date("Y") . '</em>');
 }
Example #8
0
 /**
  * @cover \Drupal\rules\Plugin\TypedDataFilter\FormatDateFilter
  */
 public function testFormatDateFilter()
 {
     $filter = $this->dataFilterManager->createInstance('format_date');
     $data = $this->typedDataManager->create(DataDefinition::create('timestamp'), 3700);
     $this->assertTrue($filter->canFilter($data->getDataDefinition()));
     $this->assertFalse($filter->canFilter(DataDefinition::create('any')));
     $this->assertEquals('string', $filter->filtersTo($data->getDataDefinition(), [])->getDataType());
     $fails = $filter->validateArguments($data->getDataDefinition(), []);
     $this->assertEquals(0, count($fails));
     $fails = $filter->validateArguments($data->getDataDefinition(), ['medium']);
     $this->assertEquals(0, count($fails));
     $fails = $filter->validateArguments($data->getDataDefinition(), ['invalid-format']);
     $this->assertEquals(1, count($fails));
     $fails = $filter->validateArguments($data->getDataDefinition(), ['custom']);
     $this->assertEquals(1, count($fails));
     $fails = $filter->validateArguments($data->getDataDefinition(), ['custom', 'Y']);
     $this->assertEquals(0, count($fails));
     /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
     $date_formatter = $this->container->get('date.formatter');
     $this->assertEquals($date_formatter->format(3700), $filter->filter($data->getDataDefinition(), $data->getValue(), []));
     $this->assertEquals($date_formatter->format(3700, 'short'), $filter->filter($data->getDataDefinition(), $data->getValue(), ['short']));
     $this->assertEquals('1970', $filter->filter($data->getDataDefinition(), $data->getValue(), ['custom', 'Y']));
     // Verify the filter works with non-timestamp data as well.
     $data = $this->typedDataManager->create(DataDefinition::create('datetime_iso8601'), "1970-01-01T10:10:10+00:00");
     $this->assertTrue($filter->canFilter($data->getDataDefinition()));
     $this->assertEquals('string', $filter->filtersTo($data->getDataDefinition(), [])->getDataType());
     $this->assertEquals('1970', $filter->filter($data->getDataDefinition(), $data->getValue(), ['custom', 'Y']));
     // Test cache dependencies of date format config entities are added in.
     $metadata = new BubbleableMetadata();
     $filter->filter($data->getDataDefinition(), $data->getValue(), ['short'], $metadata);
     $this->assertEquals(DateFormat::load('short')->getCacheTags(), $metadata->getCacheTags());
     $metadata = new BubbleableMetadata();
     $filter->filter($data->getDataDefinition(), $data->getValue(), ['custom', 'Y'], $metadata);
     $this->assertEquals([], $metadata->getCacheTags());
 }
Example #9
0
 /**
  * Retrieves the right format for a HTML5 time element.
  *
  * The format is important because these elements will not work with any other
  * format.
  *
  * @param string $element
  *   The $element to assess.
  *
  * @return string
  *   Returns the right format for the time element, or the original format
  *   if this is not a HTML5 element.
  */
 protected static function getHtml5TimeFormat($element)
 {
     switch ($element['#date_time_element']) {
         case 'time':
             return DateFormat::load('html_time')->getPattern();
         default:
             return $element['#date_time_format'];
     }
 }
Example #10
0
 /**
  * Tests all-day field.
  */
 public function testAlldayRangeField()
 {
     $field_name = $this->fieldStorage->getName();
     // Ensure field is set to a all-day field.
     $this->fieldStorage->setSetting('datetime_type', DateRangeItem::DATETIME_TYPE_ALLDAY);
     $this->fieldStorage->save();
     // Display creation form.
     $this->drupalGet('entity_test/add');
     $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Start date element found.');
     $this->assertFieldByName("{$field_name}[0][end_value][date]", '', 'End date element found.');
     $this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]/h4[contains(@class, "js-form-required")]', TRUE, 'Required markup found');
     $this->assertNoFieldByName("{$field_name}[0][value][time]", '', 'Start time element not found.');
     $this->assertNoFieldByName("{$field_name}[0][end_value][time]", '', 'End time element not found.');
     // Build up dates in the proper timezone.
     $value = '2012-12-31 00:00:00';
     $start_date = new DrupalDateTime($value, timezone_open(drupal_get_user_timezone()));
     $end_value = '2013-06-06 23:59:59';
     $end_date = new DrupalDateTime($end_value, timezone_open(drupal_get_user_timezone()));
     // Submit a valid date and ensure it is accepted.
     $date_format = DateFormat::load('html_date')->getPattern();
     $time_format = DateFormat::load('html_time')->getPattern();
     $edit = array("{$field_name}[0][value][date]" => $start_date->format($date_format), "{$field_name}[0][end_value][date]" => $end_date->format($date_format));
     $this->drupalPostForm(NULL, $edit, t('Save'));
     preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
     $id = $match[1];
     $this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
     $this->assertRaw($start_date->format($date_format));
     $this->assertNoRaw($start_date->format($time_format));
     $this->assertRaw($end_date->format($date_format));
     $this->assertNoRaw($end_date->format($time_format));
     // Verify that the default formatter works.
     $this->displayOptions['settings'] = ['format_type' => 'long', 'separator' => 'THESEPARATOR'] + $this->defaultSettings;
     entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')->setComponent($field_name, $this->displayOptions)->save();
     $start_expected = $this->dateFormatter->format($start_date->getTimestamp(), 'long');
     $start_expected_iso = $this->dateFormatter->format($start_date->getTimestamp(), 'custom', 'Y-m-d\\TH:i:s\\Z', 'UTC');
     $end_expected = $this->dateFormatter->format($end_date->getTimestamp(), 'long');
     $end_expected_iso = $this->dateFormatter->format($end_date->getTimestamp(), 'custom', 'Y-m-d\\TH:i:s\\Z', 'UTC');
     $this->renderTestEntity($id);
     $this->assertFieldByXPath('//time[@datetime="' . $start_expected_iso . '"]', $start_expected, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $start_expected, '%expected_iso' => $start_expected_iso]));
     $this->assertFieldByXPath('//time[@datetime="' . $end_expected_iso . '"]', $end_expected, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $end_expected, '%expected_iso' => $end_expected_iso]));
     $this->assertText(' THESEPARATOR ', 'Found proper separator');
     // Verify that the plain formatter works.
     $this->displayOptions['type'] = 'daterange_plain';
     $this->displayOptions['settings'] = $this->defaultSettings;
     entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')->setComponent($field_name, $this->displayOptions)->save();
     $expected = $start_date->format(DATETIME_DATETIME_STORAGE_FORMAT) . ' - ' . $end_date->format(DATETIME_DATETIME_STORAGE_FORMAT);
     $this->renderTestEntity($id);
     $this->assertText($expected, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', array('%expected' => $expected)));
     // Verify that the custom formatter works.
     $this->displayOptions['type'] = 'daterange_custom';
     $this->displayOptions['settings'] = array('date_format' => 'm/d/Y') + $this->defaultSettings;
     entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')->setComponent($field_name, $this->displayOptions)->save();
     $expected = $start_date->format($this->displayOptions['settings']['date_format']) . ' - ' . $end_date->format($this->displayOptions['settings']['date_format']);
     $this->renderTestEntity($id);
     $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', array('%expected' => $expected)));
     // Verify that the 'timezone_override' setting works.
     $this->displayOptions['type'] = 'daterange_custom';
     $this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A', 'timezone_override' => 'America/New_York'] + $this->defaultSettings;
     entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')->setComponent($field_name, $this->displayOptions)->save();
     $expected = $start_date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']);
     $expected .= ' - ' . $end_date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']);
     $this->renderTestEntity($id);
     $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', array('%expected' => $expected)));
     // Test formatters when start date and end date are the same
     $this->drupalGet('entity_test/add');
     $value = '2012-12-31 00:00:00';
     $start_date = new DrupalDateTime($value, timezone_open(drupal_get_user_timezone()));
     $end_value = '2012-12-31 23:59:59';
     $end_date = new DrupalDateTime($end_value, timezone_open(drupal_get_user_timezone()));
     $date_format = DateFormat::load('html_date')->getPattern();
     $time_format = DateFormat::load('html_time')->getPattern();
     $edit = array("{$field_name}[0][value][date]" => $start_date->format($date_format), "{$field_name}[0][end_value][date]" => $start_date->format($date_format));
     $this->drupalPostForm(NULL, $edit, t('Save'));
     preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
     $id = $match[1];
     $this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
     $this->displayOptions = ['type' => 'daterange_default', 'label' => 'hidden', 'settings' => ['format_type' => 'long', 'separator' => 'THESEPARATOR'] + $this->defaultSettings];
     entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')->setComponent($field_name, $this->displayOptions)->save();
     $start_expected = $this->dateFormatter->format($start_date->getTimestamp(), 'long');
     $start_expected_iso = $this->dateFormatter->format($start_date->getTimestamp(), 'custom', 'Y-m-d\\TH:i:s\\Z', 'UTC');
     $end_expected = $this->dateFormatter->format($end_date->getTimestamp(), 'long');
     $end_expected_iso = $this->dateFormatter->format($end_date->getTimestamp(), 'custom', 'Y-m-d\\TH:i:s\\Z', 'UTC');
     $this->renderTestEntity($id);
     $this->assertFieldByXPath('//time[@datetime="' . $start_expected_iso . '"]', $start_expected, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $start_expected, '%expected_iso' => $start_expected_iso]));
     $this->assertFieldByXPath('//time[@datetime="' . $end_expected_iso . '"]', $end_expected, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $end_expected, '%expected_iso' => $end_expected_iso]));
     $this->assertText(' THESEPARATOR ', 'Found proper separator');
     $this->displayOptions['type'] = 'daterange_plain';
     entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')->setComponent($field_name, $this->displayOptions)->save();
     $expected = $start_date->format(DATETIME_DATETIME_STORAGE_FORMAT) . ' THESEPARATOR ' . $end_date->format(DATETIME_DATETIME_STORAGE_FORMAT);
     $this->renderTestEntity($id);
     $this->assertText($expected, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', array('%expected' => $expected)));
     $this->assertText(' THESEPARATOR ', 'Found proper separator');
     $this->displayOptions['type'] = 'daterange_custom';
     $this->displayOptions['settings']['date_format'] = 'm/d/Y';
     entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')->setComponent($field_name, $this->displayOptions)->save();
     $expected = $start_date->format($this->displayOptions['settings']['date_format']) . ' THESEPARATOR ' . $end_date->format($this->displayOptions['settings']['date_format']);
     $this->renderTestEntity($id);
     $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', array('%expected' => $expected)));
     $this->assertText(' THESEPARATOR ', 'Found proper separator');
 }
 public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state)
 {
     $config = $this->config('flickr.settings');
     $apply = \Drupal::l(t('https://www.flickr.com/services/apps/create/apply'), \Drupal\Core\Url::fromUri('https://www.flickr.com/services/apps/create/apply'));
     $form['flickr_api_key'] = ['#type' => 'textfield', '#title' => t('Flickr API Key'), '#required' => TRUE, '#default_value' => $config->get('flickr_api_key'), '#description' => t("API Key from Flickr. Get an API Key at !apply.", ['!apply' => $apply])];
     // @FIXME
     // The Assets API has totally changed. CSS, JavaScript, and libraries are now
     // attached directly to render arrays using the #attached property.
     //
     //
     // @see https://www.drupal.org/node/2169605
     // @see https://www.drupal.org/node/2408597
     // drupal_add_css(drupal_get_path('module', 'flickr') . '/flickr_cc_icons.css', array(
     //     'group' => CSS_DEFAULT,
     //     'every_page' => FALSE,
     //   ));
     // A preview area.
     $form['flickr_preview'] = ['#type' => 'fieldset', '#title' => t('Preview'), '#description' => '<p>' . t('Note: Save the form to see your changes.') . '</p>', '#collapsible' => TRUE, '#collapsed' => \Drupal::config('flickr.settings')->get('flickr_preview_collapsed')];
     // Form submit resulted in an uncollapsed preview. Set it back.
     \Drupal::configFactory()->getEditable('flickr.settings')->set('flickr_preview_collapsed', TRUE)->save();
     if (\Drupal::moduleHandler()->moduleExists('flickr_filter')) {
         // @FIXME
         // Could not extract the default value because it is either indeterminate, or
         // not scalar. You'll need to provide a default value in
         // config/install/flickr.settings.yml and config/schema/flickr.schema.yml.
         $markup = \Drupal::config('flickr.settings')->get('flickr_preview_html');
         // Reset to the default preview template if it is found empty.
         $trimmed = trim($markup['value']);
         $markup = empty($trimmed) ? \Drupal::configFactory()->getEditable('flickr.settings')->set('flickr_preview_html', ['value' => '[flickr-user:id=lolandese1, size=q, num=2, sort=views]', 'format' => 'full_html'])->save() : $markup;
         // Use the current user's default format if the stored one isn't available.
         //TODO: this is wiping anything the user has entered
         $format_id = filter_default_format();
         $form['flickr_preview']['flickr_preview_markup'] = ['#markup' => '<div class="flickr-preview">' . check_markup($markup['value'], $format_id, '', $cache = FALSE) . '</div>'];
         $form['flickr_preview']['flickr_preview_details'] = ['#type' => 'fieldset', '#title' => t('Template'), '#description' => t('Wrapped in <code>&lt;div class="flickr-preview"> .. &lt;/div></code>.'), '#collapsible' => TRUE, '#collapsed' => FALSE];
         $form['flickr_preview']['flickr_preview_details']['flickr_preview_html'] = ['#type' => 'text_format', '#description' => t('Changes are visible after form submit. Empty the text area to reset to default.'), '#default_value' => $markup['value'], '#format' => $format_id, '#access' => 'use text format ' . $format_id];
     } else {
         $flickr_filter_module = \Drupal::l(t('Flickr Filter sub-module'), \Drupal\Core\Url::fromRoute('system.modules_list'));
         $form['flickr_preview']['flickr_note_preview'] = ['#markup' => t("Enable the !flickr_filter_module to have an editable preview template available to see the effect of your settings changes instantly without closing the form.", ['!flickr_filter_module' => $flickr_filter_module])];
     }
     $form['credentials'] = ['#type' => 'fieldset', '#title' => t('Flickr credentials'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 2];
     $form['flickr_api_secret'] = ['#type' => 'textfield', '#title' => t('API Shared Secret'), '#required' => TRUE, '#default_value' => \Drupal::config('flickr.settings')->get('flickr_api_secret'), '#description' => t("API key's secret from Flickr.")];
     $default = \Drupal::config('flickr.settings')->get('flickr_default_userid');
     if (!empty($default)) {
         $info = flickr_people_getinfo($default);
         $default = $info['username']['_content'];
     }
     $form['credentials']['flickr_default_userid'] = ['#type' => 'textfield', '#title' => t('Default Flickr User ID'), '#default_value' => $default, '#description' => t('An optional default Flickr user (number@number, alias, username or email). This will be used when no user is specified.')];
     // We need an api key before we can verify usernames.
     if (!$form['flickr_api_key']['#default_value']) {
         $form['credentials']['flickr_default_userid']['#disabled'] = TRUE;
         $form['credentials']['flickr_default_userid']['#description'] .= ' ' . t('Disabled until a valid API Key is set.');
     }
     $form['info_settings'] = ['#type' => 'fieldset', '#title' => t('Global options'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 4];
     $form['info_settings']['flickr_photos_per_page'] = ['#type' => 'textfield', '#title' => t('Number of photos per album'), '#required' => TRUE, '#default_value' => \Drupal::config('flickr.settings')->get('flickr_photos_per_page'), '#description' => t('How many photos of a photoset display in your nodes if no number is specified. Clear the cache on form submit.'), '#size' => 3, '#maxlength' => 3];
     $form['info_settings']['flickr_default_size_album'] = ['#type' => 'select', '#title' => t('Default size for photos in an album'), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_default_size_album'), '#options' => ['s' => t('s: 75 px square'), 't' => t('t: 100 px on longest side'), 'q' => t('q: 150 px square'), 'm' => t('m: 240 px on longest side'), 'n' => t('n: 320 px on longest side (!)'), '-' => t('-: 500 px on longest side'), 'z' => t('z: 640 px on longest side'), 'c' => t('c: 800 px on longest side (!)'), 'b' => t('b: 1024 px on longest side')], '#description' => t("A default Flickr size to use if no size is specified, for example [flickr-photoset:id=72157634563269642].<br />Clear the cache on form submit.<br />!: TAKE CARE, the 'c' size (800px) is missing on Flickr images uploaded before March 1, 2012!")];
     $guidelines = \Drupal::l(t('Guidelines'), \Drupal\Core\Url::fromUri('https://www.flickr.com/guidelines.gne/'));
     $attribution = \Drupal::l(t('proper attribution'), \Drupal\Core\Url::fromUri('https://www.flickr.com/services/developer/attributions/'));
     $form['info_settings']['flickr_title_suppress_on_small'] = ['#type' => 'textfield', '#title' => t('Minimum image width to display a title caption'), '#required' => TRUE, '#default_value' => \Drupal::config('flickr.settings')->get('flickr_title_suppress_on_small'), '#description' => t("Small images have liitle space for a title caption. Replace it with the text 'Flickr' that links to the photo page on Flickr to comply with their !guidelines.<br />Set it to '0 px' to always include or '999 px' to always exclude. To give !attribution this should be included (space allowing). Clear the cache on form submit.", ['!attribution' => $attribution, '!guidelines' => $guidelines]), '#field_suffix' => t('px'), '#size' => 3, '#maxlength' => 3, '#attributes' => ['class' => ['flickr-form-align']]];
     $form['info_settings']['flickr_metadata_suppress_on_small'] = ['#type' => 'textfield', '#title' => t('Minimum image width to display date, location, photographer and optionally license info under the caption'), '#required' => TRUE, '#default_value' => \Drupal::config('flickr.settings')->get('flickr_metadata_suppress_on_small'), '#description' => t("Suppress extra info on small images. Set it to '0 px' to always include or '999 px' to always exclude. To give !attribution this should be included (space allowing). Clear the cache on form submit.", ['!attribution' => $attribution]), '#field_suffix' => t('px'), '#size' => 3, '#maxlength' => 3, '#attributes' => ['class' => ['flickr-form-align']]];
     $rubular = \Drupal::l(t('http://rubular.com/r/RhKjj9Thy1'), \Drupal\Core\Url::fromUri('http://rubular.com/r/RhKjj9Thy1'));
     $form['info_settings']['flickr_regex'] = ['#type' => 'textfield', '#title' => t("Replace photo titles matching this Regular Expression with 'View on Flickr'"), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_regex'), '#description' => t("Avoids camera generated titles like 'IMG_4259'. Try your own RegEx first with !rubular. Leave empty to NOT replace any titles.", ['!rubular' => $rubular]), '#field_prefix' => t('/'), '#field_suffix' => t('/'), '#size' => 60];
     $cc_icons = \Drupal::l(t('Source for the CC icon font'), \Drupal\Core\Url::fromUri('https://cc-icons.github.io/'));
     $cc_example_cna = '<span class="flickr-cc">' . \Drupal::l('cna', \Drupal\Core\Url::fromUri('https://creativecommons.org/licenses/by-nc-sa/2.0/')) . '</span>';
     $cc_example_copy = '<span class="flickr-copyright">' . \Drupal::l('©', \Drupal\Core\Url::fromUri('https://en.wikipedia.org/wiki/Copyright')) . '</span>';
     $cc_example_p = '<span class="flickr-cc">' . \Drupal::l('p', \Drupal\Core\Url::fromUri('https://flickr.com/commons/usage/')) . '</span>';
     $form['info_settings']['flickr_license'] = ['#type' => 'radios', '#title' => t("License icon"), '#options' => [t("No"), t("On the image on mouse-over only (small in the top left corner, on hover). NOTE: Does not display with the Flickr Style 'Enlarge'."), t("On the image (small in the top left corner, always)"), t("In the caption")], '#default_value' => \Drupal::config('flickr.settings')->get('flickr_license'), '#description' => t("To give !attribution. Clear the cache on form submit.<br />!cc_icons in 'flickr_cc_icons.css'. Download it if you prefer to host it yourself locally (CC 4.0 licensed, give credit somewhere). Adjust 'flickr_cc_icons.css' accordingly.<p>Some examples (try to mouse-over):</p>!ccexample_cna !cc_example_copy !cc_example_p", ['!attribution' => $attribution, '!cc_icons' => $cc_icons, '!ccexample_cna' => $cc_example_cna, '!cc_example_copy' => $cc_example_copy, '!cc_example_p' => $cc_example_p])];
     $form['info_settings']['flickr_restrict'] = ['#type' => 'radios', '#title' => t("License restriction for 'public' queries"), '#options' => [t("Always restrict 'public' queries to only Creative Commons licensed media."), t("Do not restrict media to Creative Commons licensed on 'public' queries if no results are returned."), t("Do not restrict media to Creative Commons licensed on 'public' queries.")], '#default_value' => \Drupal::config('flickr.settings')->get('flickr_restrict'), '#description' => t("With 'public' queries are intended Flickr requests that do not specify a Flickr user or group ID, thus returning results from all public Flickr photos.")];
     $form['info_settings']['flickr_extend'] = ['#type' => 'checkbox', '#title' => t("Extend the tag filter to search for matching terms also in the Flickr photo title and description besides Flickr tags. Descriptions are only searched on the album type 'user' (also 'public')."), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_extend'), '#description' => t("Shows more results. Can be overridden individually by the filter tag, eg. [flickr-user:id=public, size=q, tags=Augusto Canario, extend=true] or in the specific configuration of a Flickr block.")];
     $form['info_settings']['flickr_maps'] = ['#type' => 'checkbox', '#title' => t('Extra links to Flickr maps'), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_maps'), '#description' => t('Include extra links to maps available for a user, group or set on Flickr. Locations mentioned (if displayed) under individual images link to corresponding Flickr user maps in any case, independent of the setting here.')];
     $form['info_settings']['flickr_counter'] = ['#type' => 'checkbox', '#title' => t('Show a Flickr counter'), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_counter'), '#description' => t('Shows how many photos are displayed out of the total number available for a user, group, set or tags on Flickr. Can be overridden individually by the filter tag, eg. [flickr-photoset:id=72157634563269642,count=false]')];
     $form['info_settings']['flickr_thousands_sep'] = ['#type' => 'textfield', '#title' => t('Counter thousands separator'), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_thousands_sep'), '#description' => t('Common values are NULL, a dot ("."), a comma (",") or a space (" ").'), '#field_prefix' => t('For example <em>4 out of 23</em>'), '#field_suffix' => t('<em>473</em>'), '#size' => 1, '#maxlength' => 1];
     // @FIXME
     // Could not extract the default value because it is either indeterminate, or
     // not scalar. You'll need to provide a default value in
     // config/install/flickr.settings.yml and config/schema/flickr.schema.yml.
     $form['info_settings']['flickr_geophp'] = ['#type' => 'checkboxes', '#title' => t('Use Google instead of Flickr for location info (reverse geocoding)'), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_geophp'), '#options' => ['title' => t('In the album title'), 'caption' => t('In the photo caption')]];
     $form['date_formats_settings'] = ['#type' => 'fieldset', '#title' => t('Date formats'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 6];
     // Get list of all available date formats.
     $formats = [];
     $date_types = DateFormat::loadMultiple();
     $date_formatter = \Drupal::service('date.formatter');
     foreach ($date_types as $machine_name => $format) {
         // @FIXME
         // // @FIXME
         //      $formats[$machine_name] = t('@name format', array('@name' => $format->label)) . ': ' .$date_formatter->format(REQUEST_TIME, $machine_name);
         // // The correct configuration object could not be determined. You'll need to
         // // rewrite this call manually.
         // if (($format_string = variable_get('date_format_' . $f, FALSE)) === FALSE) {
         //       $format_string = key(system_get_date_formats($f));
         //     }
         if (!empty($format_string)) {
             $formats[$f] = $format['title'] . ' [' . format_date(REQUEST_TIME, 'custom', $format_string) . ']';
         }
     }
     $formats['interval'] = 'Time ago [' . \Drupal::service("date.formatter")->formatInterval(3600 * 24 * 90, 1) . ' ago]';
     $form['date_formats_settings']['flickr_date_format_image_title'] = ['#type' => 'select', '#title' => t('When hovering an image (mouse-over)'), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_date_format_image_title'), '#options' => $formats, '#empty_option' => t('- None -'), '#empty_value' => 'none'];
     $form['date_formats_settings']['flickr_date_format_image_caption'] = ['#type' => 'select', '#title' => t('In the image caption'), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_date_format_image_caption'), '#options' => $formats, '#empty_option' => t('- None -'), '#empty_value' => 'none'];
     $form['date_formats_settings']['flickr_date_format_image_caption_hover'] = ['#type' => 'select', '#title' => t('When hovering a date in the caption'), '#description' => t("If you don't want to display anything when hovering the date, select 'None'."), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_date_format_image_caption_hover'), '#options' => $formats, '#empty_option' => t('- None -'), '#empty_value' => 'none'];
     // Disable the caption hover option if a date in the caption is set to 'none'.
     if (\Drupal::config('flickr.settings')->get('flickr_date_format_image_caption') == 'none') {
         $form['date_formats_settings']['flickr_date_format_image_caption_hover']['#disabled'] = TRUE;
         $form['date_formats_settings']['flickr_date_format_image_caption_hover']['#description'] = t('Disabled until a date format for the image caption is selected.');
     }
     $form['date_formats_settings']['flickr_date_format_album_title'] = ['#type' => 'select', '#title' => t('In the album title'), '#description' => t("If the selected date format contains a time, only the date part of it will be used in the album title."), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_date_format_album_title'), '#options' => $formats, '#empty_option' => t('- None -'), '#empty_value' => 'none'];
     $colorbox_module = \Drupal::l(t('Colorbox module'), \Drupal\Core\Url::fromUri('https://drupal.org/project/colorbox'));
     $form['overlay_settings'] = ['#type' => 'fieldset', '#title' => t('Overlay browser (Colorbox, Lightbox)'), '#description' => t('Recommended is the !colorbox_module. Leave empty to link directly to the Flickr photo page instead of opening the bigger version of the image.', ['!colorbox_module' => $colorbox_module]), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 8];
     $form['overlay_settings']['flickr_class'] = ['#type' => 'textfield', '#title' => t('class'), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_class'), '#description' => t('For example: <em>colorbox</em>. Can be left empty for Lightbox. Clear the cache on form submit.')];
     $form['overlay_settings']['flickr_rel'] = ['#type' => 'textfield', '#title' => t('rel'), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_rel'), '#description' => t('For example: <em>gallery-all</em> for Colorbox or <em>lightbox[gallery]</em>. Clear the cache on form submit.')];
     $form['overlay_settings']['flickr_opening_size'] = ['#type' => 'select', '#title' => t('Image size to open'), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_opening_size'), '#options' => ['q' => t('q: 150 px square'), 'm' => t('m: 240 px on longest side'), 'n' => t('n: 320 px on longest side (!)'), '' => t('-: 500 px on longest side'), 'z' => t('z: 640 px on longest side'), 'c' => t('c: 800 px on longest side (!)'), 'b' => t('b: 1024 px on longest side'), 'h' => t('h: 1600 px on longest side')], '#description' => t("The image size to open in the overlay browser when clicking the image. Larger sizes make navigating to next and previous pictures slower.<br />Clear the cache on form submit.<br />!: TAKE CARE, the 'c' size (800px) is missing on Flickr images uploaded before March 1, 2012!")];
     // @FIXME
     // Could not extract the default value because it is either indeterminate, or
     // not scalar. You'll need to provide a default value in
     // config/install/flickr.settings.yml and config/schema/flickr.schema.yml.
     $form['overlay_settings']['flickr_info_overlay'] = ['#type' => 'checkboxes', '#title' => t('Info to include when enlarging the image'), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_info_overlay'), '#description' => t("To give !attribution all marked * should be checked. Clear the cache on form submit.", ['!attribution' => $attribution]), '#options' => ['title' => t('Title *'), 'metadata' => t('Date, location and photographer *'), 'description' => t("Description, applies also on the text that shows on mouseover (the image 'title' attribute)"), 'license' => t('License info *')]];
     $form['css_settings'] = ['#type' => 'fieldset', '#title' => t('Styling (CSS related)'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 10];
     $autofloat_module = \Drupal::l(t('AutoFloat module'), \Drupal\Core\Url::fromUri('https://drupal.org/project/autofloat'));
     $form['css_settings']['flickr_css'] = ['#type' => 'checkbox', '#title' => t('Use flickr.css'), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_css'), '#description' => t("Uncheck to take care of the styling yourself in custom CSS. If you use Flickr Filter, you might find the !autofloat_module useful.", ['!autofloat_module' => $autofloat_module])];
     $form['css_settings']['css_variables'] = ['#type' => 'fieldset', '#title' => t('CSS variables'), '#collapsible' => TRUE, '#collapsed' => FALSE];
     $style_settings_module = \Drupal::l(t('Style (CSS) Settings module'), \Drupal\Core\Url::fromUri('https://drupal.org/project/style_settings'));
     if (\Drupal::moduleHandler()->moduleExists('style_settings')) {
         $form['css_settings']['css_variables']['#collapsed'] = TRUE;
         $form['css_settings']['css_variables']['flickr_capsize'] = ['#type' => 'fieldset', '#title' => t('Caption font-size'), '#description' => t('Relative to the font size for the normal text. A minimum font size setting of your browser might limit it. Test it on different browsers.'), '#attributes' => ['class' => ['container-inline']]];
         // Number field without a '#field_suffix'.
         $form['css_settings']['css_variables']['flickr_capsize']['flickr_capsize_value'] = ['#type' => 'style_settings_number', '#default_value' => \Drupal::config('flickr.settings')->get('flickr_capsize_value')];
         // A measurement unit select field.
         $form['css_settings']['css_variables']['flickr_capsize']['flickr_capsize_unit'] = ['#type' => 'select', '#options' => ['px' => t('px'), 'em' => t('em'), '%' => t('%')], '#default_value' => \Drupal::config('flickr.settings')->get('flickr_capsize_unit'), '#required' => TRUE];
         $form['css_settings']['css_variables']['flickr_sswidth'] = ['#type' => 'fieldset', '#title' => t('Slideshow width'), '#description' => t('Relative to width of the containing block element (%) or fixed (px). Never wider than the containing block (max-width: 100 %).'), '#attributes' => ['class' => ['container-inline']]];
         // Number field without a '#field_suffix'.
         $form['css_settings']['css_variables']['flickr_sswidth']['flickr_sswidth_value'] = ['#type' => 'style_settings_number', '#default_value' => \Drupal::config('flickr.settings')->get('flickr_sswidth_value')];
         // A measurement unit select field.
         $form['css_settings']['css_variables']['flickr_sswidth']['flickr_sswidth_unit'] = ['#type' => 'select', '#options' => ['px' => t('px'), '%' => t('%')], '#default_value' => \Drupal::config('flickr.settings')->get('flickr_sswidth_unit'), '#required' => TRUE];
         $form['css_settings']['css_variables']['flickr_ssratio'] = ['#type' => 'fieldset', '#title' => t('Slideshow width:height ratio'), '#attributes' => ['class' => ['container-inline']]];
         // Number field without a '#field_suffix'.
         $form['css_settings']['css_variables']['flickr_ssratio']['flickr_sswratio'] = ['#type' => 'style_settings_number', '#default_value' => \Drupal::config('flickr.settings')->get('flickr_sswratio'), '#field_suffix' => '&nbsp;&nbsp;:&nbsp;&nbsp;', '#step' => 1, '#min' => 1];
         // Number field without a '#field_suffix'.
         $form['css_settings']['css_variables']['flickr_ssratio']['flickr_sshratio'] = ['#type' => 'style_settings_number', '#default_value' => \Drupal::config('flickr.settings')->get('flickr_sshratio'), '#step' => 1, '#min' => 1, '#attributes' => NULL, '#input_help' => NULL];
     } elseif (!\Drupal::moduleHandler()->moduleExists('flickrstyle')) {
         $form['css_settings']['css_variables']['flickr_note'] = ['#markup' => t("Enable the !style_settings_module (<strong>dev version!</strong>) to get even more styling options. They consist of:<ul>\n          <li>the photo caption font size</li>\n          <li>the slideshow width, fluid (%) or fixed (px)</li>\n          <li>the slideshow width/height ratio</li>\n        </ul>", ['!style_settings_module' => $style_settings_module])];
     } else {
         $form['css_settings']['css_variables']['flickr_note'] = ['#markup' => t("Enable the !style_settings_module (<strong>dev version!</strong>) to get even more styling options. They consist of:<ul>\n          <li>the photo caption font size</li>\n          <li>the slideshow width, fluid (%) or fixed (px)</li>\n          <li>the slideshow width/height ratio</li>\n          <li>customized rounded corners, shadow, border and scale properties</li>\n        </ul>", ['!style_settings_module' => $style_settings_module])];
     }
     $flickr_style = \Drupal::l(t('Flickr Style'), \Drupal\Core\Url::fromRoute('system.modules_list'));
     if (!\Drupal::moduleHandler()->moduleExists('flickrstyle')) {
         $form['css_settings']['flickr_style'] = ['#markup' => '<p>' . t("Extend the styling options with rounded corners, shadow, border and emphasize on hover by enabling the !flickr_style sub-module.", ['!flickr_style' => $flickr_style]) . '</p>', '#weight' => -1];
     }
     $form['advanced_settings'] = ['#type' => 'fieldset', '#title' => t('Advanced'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 12];
     $times = [900, 1800, 2700, 3600, 7200, 10800, 14400, 18000, 21600, 43200, 86400];
     $ageoptions = array_combine($times, 'format_interval', $times);
     $form['advanced_settings']['flickr_cache_duration'] = ['#type' => 'select', '#title' => t('Update interval'), '#options' => $ageoptions, '#default_value' => \Drupal::config('flickr.settings')->get('flickr_cache_duration'), '#description' => t("The refresh interval indicating how often you want to check cached Flickr API calls are up to date. This only kicks in when a repeating request with the same query is made, e.g. when re-saving a Flickr block without changing the parameters within the above specified interval. A Flickr API request is usually avoided by a page or block cache, therefore it is pretty safe to set it to '<em>1 hour</em>'.")];
     $cache_warming = \Drupal::l(t('cache warming'), \Drupal\Core\Url::fromUri('https://drupal.org/node/1576686'));
     $form['advanced_settings']['flickr_per_page'] = ['#type' => 'textfield', '#title' => t('Limit the number of photos to grab for random and popularity sort'), '#required' => TRUE, '#default_value' => \Drupal::config('flickr.settings')->get('flickr_per_page'), '#description' => t('Setting a lower number enhances performance but makes random results being less spread between one another (not less random) and returns popular (most viewed on Flickr) only from the <em>n</em> most recent.<br />Minimum 20, maximum 500. Set the maximum only if you use !cache_warming.', ['!cache_warming' => $cache_warming]), '#size' => 3, '#maxlength' => 3];
     $more_info = \Drupal::l(t('More info'), \Drupal\Core\Url::fromUri('https://stackoverflow.com/a/4635991'));
     $form['advanced_settings']['flickr_curl'] = ['#type' => 'checkbox', '#title' => t("Use 'cURL' to determine the image width instead of 'fopen' used by the PHP function 'getimagesize'."), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_curl'), '#description' => t("Your server configuration now uses 'fopen' for external resources (used by 'getimagesize'). 'cURL' might be faster. !more_info.", ['!more_info' => $more_info])];
     $form['advanced_settings']['flickr_curl2'] = ['#type' => 'checkbox', '#title' => t("Use 'cURL' instead of 'stream_socket_client' (drupal_http_request) to make data requests."), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_curl2'), '#description' => t("Otherwise cURL will only be used as fallback when drupal_http_request fails.")];
     // Offer the following setting if both allow_url_fopen and curl are enabled.
     if (!ini_get("allow_url_fopen") || !function_exists('curl_version')) {
         $form['advanced_settings']['flickr_curl']['#disabled'] = TRUE;
         if (function_exists('curl_version')) {
             $form['advanced_settings']['flickr_curl']['#description'] = t("Disabled because your server configuration only uses 'cURL' (not 'fopen')");
         } elseif (ini_get("allow_url_fopen")) {
             $form['advanced_settings']['flickr_curl']['#description'] = t("Disabled because your server configuration only uses 'fopen' (not 'cURL')");
         } else {
             $form['advanced_settings']['flickr_curl']['#description'] = t("It could not be determined if your server configuration uses 'fopen' or 'cURL'. You might see unnecessary whitespace next to your floating images. It probably means your server does not allow neither 'fopen' nor 'cURL'. Check your 'php.ini' settings first, then contact your hosting company.");
         }
     }
     // Do not offer the following setting if curl is not available.
     if (!function_exists('curl_version')) {
         $form['advanced_settings']['flickr_curl2']['#disabled'] = TRUE;
         $form['advanced_settings']['flickr_curl2']['#description'] = t("Disabled because your server configuration only allows 'stream_socket_client' (not 'cURL')");
     }
     $devel_module = \Drupal::l(t('Devel module'), \Drupal\Core\Url::fromUri('https://drupal.org/project/devel'));
     // Disable the Devel output until it is available.
     if (!\Drupal::moduleHandler()->moduleExists('devel')) {
         if (\Drupal::config('flickr.settings')->get('flickr_debug') == 2) {
             \Drupal::configFactory()->getEditable('flickr.settings')->set('flickr_debug', 1)->save();
             drupal_set_message(t("The debug output has been set to 'Flickr response only'. 'Plus Devel' has been disabled until you enable the !devel_module.", ['!devel_module' => $devel_module]), 'warning', FALSE);
         }
     }
     $form['advanced_settings']['flickr_debug'] = ['#type' => 'radios', '#title' => t('Enable Debug Output'), '#options' => [t('None'), t('Flickr response only (as a link to an XML page in a debug message)'), t('Plus Devel (Flickr response plus additional output)')], '#description' => t('Display the Flickr XML response, all passed photo/album arguments and HTTP requests/response objects via the !devel_module.', ['!devel_module' => $devel_module]), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_debug')];
     // Disable the Devel output until it is available.
     if (!\Drupal::moduleHandler()->moduleExists('devel')) {
         $form['advanced_settings']['flickr_debug'][2]['#disabled'] = TRUE;
         $form['advanced_settings']['flickr_debug']['#description'] = t('Display the Flickr XML response.');
     }
     $form['block_settings'] = ['#type' => 'fieldset', '#title' => t('Block options'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 16];
     if (\Drupal::moduleHandler()->moduleExists('flickr_block')) {
         $form['block_settings']['#description'] = t('Clear the cache on form submit.');
     }
     $date = \Drupal::l(t('Date'), \Drupal\Core\Url::fromUri('https://www.drupal.org/project/date'));
     $geofield = \Drupal::l(t('Geofield'), \Drupal\Core\Url::fromUri('https://www.drupal.org/project/geofield'));
     $form['block_settings']['flickr_smart'] = ['#type' => 'checkbox', '#title' => t("Smart install of Flickr Block"), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_smart'), '#description' => t("On install of Flickr Block auto create Flickr taxonomy, date and geo fields on all node types to grab Flickr photos related to the node on the same page as a Flickr block based on tags, a date or a location. Enable Taxonomy (core), !date (including date_popup) and !geofield before enabling Flickr Block for the first time (or uninstall it first).", ['!date' => $date, '!geofield' => $geofield])];
     // If a block variable exists, the Flickr Block module has not been
     // uninstalled, thus disable the setting relevant only during installation.
     global $conf;
     if (\Drupal::moduleHandler()->moduleExists('flickr_block') || isset($conf['flickr_block_photos_per_set'])) {
         $form['block_settings']['flickr_smart']['#disabled'] = TRUE;
         $form['block_settings']['flickr_smart']['#title'] = '<span class="grayed-out">' . t("Smart install of Flickr Block") . '</span> | ' . t('Disabled until uninstall of Flickr Block.');
         $form['block_settings']['flickr_smart']['#description'] = '<span class="grayed-out">' . t("On install of Flickr Block auto create Flickr taxonomy, date and geo fields on all node types to grab Flickr photos related to the node on the same page as a Flickr block based on tags, a date or a location. Enable Taxonomy (core), !date (including date_popup) and !geofield before enabling Flickr Block for the first time (or uninstall it first).", ['!date' => $date, '!geofield' => $geofield]) . '</span>';
     }
     $flickr_block = \Drupal::l(t('Flickr Block'), \Drupal\Core\Url::fromRoute('system.modules_list'));
     if (!\Drupal::moduleHandler()->moduleExists('flickr_block')) {
         $form['block_settings']['flickr_block'] = ['#markup' => t("Display Flickr photos in blocks by enabling the !flickr_block sub-module.", ['!flickr_style' => $flickr_style])];
     }
     $form['flickr_cc'] = ['#type' => 'checkbox', '#title' => t("Flush the cache on form submit to see your changes instantly."), '#default_value' => \Drupal::config('flickr.settings')->get('flickr_cc'), '#description' => t("Note that form submit will be slower. Your content will be rebuilt at the first visit. Your choice will be 'remembered' for your next visit to this configuration page."), '#weight' => 97];
     if (\Drupal::config('flickr.settings')->get('flickr_css') && \Drupal::moduleHandler()->moduleExists('style_settings')) {
         $form['flickr_cc']['#title'] = t("Flush the cache on form submit to see your changes instantly. CSS is flushed in any case.");
     }
     // Call submit_function() on form submission.
     $form['#submit'][] = 'flickr_admin_settings_submit';
     return parent::buildForm($form, $form_state);
 }
 /**
  * @cover replacePlaceHolders
  */
 public function testBubbleableMetadata()
 {
     // Make sure the bubbleable metadata added by the fetcher is properly passed
     // though.
     $bubbleable_metadata = new BubbleableMetadata();
     // Save the node, so it gets a cache tag.
     $this->node->save();
     $this->placeholderResolver->replacePlaceHolders('test {{node.field_integer}}', ['node' => $this->node->getTypedData()], $bubbleable_metadata);
     $expected = ['node:' . $this->node->id()];
     $this->assertEquals($expected, $bubbleable_metadata->getCacheTags());
     // Ensure cache tags of filters are added in.
     $bubbleable_metadata = new BubbleableMetadata();
     $this->placeholderResolver->replacePlaceHolders("test {{ node.created.value | format_date('medium') }}", ['node' => $this->node->getTypedData()], $bubbleable_metadata);
     $expected = Cache::mergeTags(['node:' . $this->node->id()], DateFormat::load('medium')->getCacheTags());
     $this->assertEquals($expected, $bubbleable_metadata->getCacheTags());
 }
Example #13
0
 /**
  * Tests date and time field.
  */
 function testDatetimeField()
 {
     $field_name = $this->fieldStorage->getName();
     // Change the field to a datetime field.
     $this->fieldStorage->setSetting('datetime_type', 'datetime');
     $this->fieldStorage->save();
     // Display creation form.
     $this->drupalGet('entity_test/add');
     $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
     $this->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.');
     // Build up a date in the UTC timezone.
     $value = '2012-12-31 00:00:00';
     $date = new DrupalDateTime($value, 'UTC');
     // Update the timezone to the system default.
     $date->setTimezone(timezone_open(drupal_get_user_timezone()));
     // Submit a valid date and ensure it is accepted.
     $date_format = DateFormat::load('html_date')->getPattern();
     $time_format = DateFormat::load('html_time')->getPattern();
     $edit = array("{$field_name}[0][value][date]" => $date->format($date_format), "{$field_name}[0][value][time]" => $date->format($time_format));
     $this->drupalPostForm(NULL, $edit, t('Save'));
     preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
     $id = $match[1];
     $this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
     $this->assertRaw($date->format($date_format));
     $this->assertRaw($date->format($time_format));
     // Verify that the date is output according to the formatter settings.
     $options = array('format_type' => array('short', 'medium', 'long'));
     foreach ($options as $setting => $values) {
         foreach ($values as $new_value) {
             // Update the entity display settings.
             $this->displayOptions['settings'] = array($setting => $new_value) + $this->defaultSettings;
             entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')->setComponent($field_name, $this->displayOptions)->save();
             $this->renderTestEntity($id);
             switch ($setting) {
                 case 'format_type':
                     // Verify that a date is displayed.
                     $expected = format_date($date->getTimestamp(), $new_value);
                     $expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\\TH:i:s\\Z', 'UTC');
                     $this->renderTestEntity($id);
                     $this->assertFieldByXPath('//time[@datetime="' . $expected_iso . '"]', $expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', array('%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso)));
                     break;
             }
         }
     }
     // Verify that the plain formatter works.
     $this->displayOptions['type'] = 'datetime_plain';
     $this->displayOptions['settings'] = $this->defaultSettings;
     entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')->setComponent($field_name, $this->displayOptions)->save();
     $expected = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
     $this->renderTestEntity($id);
     $this->assertText($expected, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', array('%expected' => $expected)));
     // Verify that the 'datetime_custom' formatter works.
     $this->displayOptions['type'] = 'datetime_custom';
     $this->displayOptions['settings'] = array('date_format' => 'm/d/Y g:i:s A') + $this->defaultSettings;
     entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')->setComponent($field_name, $this->displayOptions)->save();
     $expected = $date->format($this->displayOptions['settings']['date_format']);
     $this->renderTestEntity($id);
     $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', array('%expected' => $expected)));
     // Verify that the 'timezone_override' setting works.
     $this->displayOptions['type'] = 'datetime_custom';
     $this->displayOptions['settings'] = array('date_format' => 'm/d/Y g:i:s A', 'timezone_override' => 'America/New_York') + $this->defaultSettings;
     entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')->setComponent($field_name, $this->displayOptions)->save();
     $expected = $date->format($this->displayOptions['settings']['date_format'], array('timezone' => 'America/New_York'));
     $this->renderTestEntity($id);
     $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', array('%expected' => $expected)));
     // Verify that the 'datetime_time_ago' formatter works for intervals in the
     // past.  First update the test entity so that the date difference always
     // has the same interval.  Since the database always stores UTC, and the
     // interval will use this, force the test date to use UTC and not the local
     // or user timezome.
     $timestamp = REQUEST_TIME - 87654321;
     $entity = EntityTest::load($id);
     $field_name = $this->fieldStorage->getName();
     $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
     $entity->{$field_name}->value = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
     $entity->save();
     $this->displayOptions['type'] = 'datetime_time_ago';
     $this->displayOptions['settings'] = array('future_format' => '@interval from now', 'past_format' => '@interval earlier', 'granularity' => 3);
     entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')->setComponent($field_name, $this->displayOptions)->save();
     $expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], ['@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])]);
     $this->renderTestEntity($id);
     $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected)));
     // Verify that the 'datetime_time_ago' formatter works for intervals in the
     // future.  First update the test entity so that the date difference always
     // has the same interval.  Since the database always stores UTC, and the
     // interval will use this, force the test date to use UTC and not the local
     // or user timezome.
     $timestamp = REQUEST_TIME + 87654321;
     $entity = EntityTest::load($id);
     $field_name = $this->fieldStorage->getName();
     $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
     $entity->{$field_name}->value = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
     $entity->save();
     entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')->setComponent($field_name, $this->displayOptions)->save();
     $expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], ['@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])]);
     $this->renderTestEntity($id);
     $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected)));
 }