本文整理汇总了PHP中Drupal\Core\Datetime\DateFormatter类的典型用法代码示例。如果您正苦于以下问题:PHP DateFormatter类的具体用法?PHP DateFormatter怎么用?PHP DateFormatter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DateFormatter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: buildRow
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity)
{
/* @var $entity \Drupal\commerce_order\Entity\Order */
$orderType = OrderType::load($entity->bundle());
$row = ['order_id' => $entity->id(), 'type' => $orderType->label(), 'customer' => ['data' => ['#theme' => 'username', '#account' => $entity->getOwner()]], 'state' => $entity->getState()->getLabel(), 'created' => $this->dateFormatter->format($entity->getCreatedTime(), 'short')];
return $row + parent::buildRow($entity);
}
示例2: getQuestion
/**
* {@inheritdoc}
*/
public function getQuestion()
{
if ($this->entityRevision instanceof TimestampedRevisionInterface) {
return t('Are you sure you want to delete the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->entityRevision->getRevisionCreationTime())]);
} else {
return t('Are you sure you want to delete to the revision');
}
}
示例3: buildRow
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity)
{
$row = array();
$row['label']['data'] = array('#type' => 'link', '#title' => SafeMarkup::checkPlain($entity->label()), '#url' => $entity->urlInfo());
$row['type'] = SafeMarkup::checkPlain($entity->get('type')->entity->label());
$row['changed'] = $this->dateFormatter->format($entity->get('changed')->value, 'short');
return $row + parent::buildRow($entity);
}
示例4: form
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state)
{
$form['label'] = array('#type' => 'textfield', '#title' => 'Name', '#maxlength' => 100, '#description' => t('Name of the date format'), '#default_value' => $this->entity->label());
$form['id'] = array('#type' => 'machine_name', '#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'), '#disabled' => !$this->entity->isNew(), '#default_value' => $this->entity->id(), '#machine_name' => array('exists' => array($this, 'exists'), 'replace_pattern' => '([^a-z0-9_]+)|(^custom$)', 'error' => $this->t('The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".')));
$form['date_format_pattern'] = array('#type' => 'textfield', '#title' => t('Format string'), '#maxlength' => 100, '#description' => $this->t('A user-defined date format. See the <a href="@url">PHP manual</a> for available options.', array('@url' => 'http://php.net/manual/function.date.php')), '#required' => TRUE, '#attributes' => ['data-drupal-date-formatter' => 'source'], '#field_suffix' => ' <small class="js-hide" data-drupal-date-formatter="preview">' . $this->t('Displayed as %date_format', ['%date_format' => '']) . '</small>');
$form['langcode'] = array('#type' => 'language_select', '#title' => t('Language'), '#languages' => LanguageInterface::STATE_ALL, '#default_value' => $this->entity->language()->getId());
$form['#attached']['drupalSettings']['dateFormats'] = $this->dateFormatter->getSampleDateFormats();
$form['#attached']['library'][] = 'system/drupal.system.date';
return parent::form($form, $form_state);
}
示例5: testPreRender
/**
* @covers ::preRender
*/
public function testPreRender()
{
$payment_status_created = mt_rand();
$payment_status = $this->getMock(PaymentStatusInterface::class);
$payment_status->expects($this->atLeastOnce())->method('getCreated')->willReturn($payment_status_created);
$this->dateFormatter->expects($this->once())->method('format')->with($payment_status_created);
$element = array('#payment_statuses' => [$payment_status]);
$build = $this->sut->preRender($element);
$this->assertSame('table', $build['table']['#type']);
}
示例6: buildRow
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity)
{
if ($entity->isLocked()) {
$row['id'] = $this->t('@entity_id (locked)', array('@entity_id' => $entity->id()));
} else {
$row['id'] = $entity->id();
}
$row['label'] = $this->getLabel($entity);
$row['pattern'] = $this->dateFormatter->format(REQUEST_TIME, $entity->id());
return $row + parent::buildRow($entity);
}
示例7: buildRow
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity)
{
$uri = $entity->urlInfo();
$options = $uri->getOptions();
$uri->setOptions($options);
$row['title']['data'] = ['#type' => 'link', '#title' => $entity->label(), '#url' => $uri];
$row['type'] = SafeMarkup::checkPlain($entity->getType()->label());
$row['author']['data'] = ['#theme' => 'username', '#account' => $entity->getOwner()];
$row['status'] = $entity->isActive() ? $this->t('active') : $this->t('not active');
$row['imported'] = $this->dateFormatter->format($entity->getImportedTime(), 'short');
$row['operations']['data'] = $this->buildOperations($entity);
return $row + parent::buildRow($entity);
}
示例8: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items)
{
$elements = array();
foreach ($items as $delta => $item) {
if ($item->value) {
$updated = $this->t('@time ago', array('@time' => $this->dateFormatter->formatInterval(REQUEST_TIME - $item->value)));
} else {
$updated = $this->t('never');
}
$elements[$delta] = array('#markup' => $updated);
}
return $elements;
}
示例9: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$config = $this->config('system.cron');
$form['description'] = array('#markup' => '<p>' . t('Cron takes care of running periodic tasks like checking for updates and indexing content for search.') . '</p>');
$form['run'] = array('#type' => 'submit', '#value' => t('Run cron'), '#submit' => array(array($this, 'submitCron')));
$status = '<p>' . t('Last run: %cron-last ago.', array('%cron-last' => $this->dateFormatter->formatInterval(REQUEST_TIME - $this->state->get('system.cron_last')))) . '</p>';
$form['status'] = array('#markup' => $status);
$form['cron_url'] = array('#markup' => '<p>' . t('To run cron from outside the site, go to <a href="!cron">!cron</a>', array('!cron' => url('cron/' . $this->state->get('system.cron_key'), array('absolute' => TRUE)))) . '</p>');
$form['cron'] = array('#title' => t('Cron settings'), '#type' => 'details', '#open' => TRUE);
$options = array(3600, 10800, 21600, 43200, 86400, 604800);
$form['cron']['cron_safe_threshold'] = array('#type' => 'select', '#title' => t('Run cron every'), '#description' => t('More information about setting up scheduled tasks can be found by <a href="@url">reading the cron tutorial on drupal.org</a>.', array('@url' => url('http://drupal.org/cron'))), '#default_value' => $config->get('threshold.autorun'), '#options' => array(0 => t('Never')) + array_map(array($this->dateFormatter, 'formatInterval'), array_combine($options, $options)));
return parent::buildForm($form, $form_state);
}
示例10: settingsForm
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state)
{
$form['num'] = array('#type' => 'number', '#title' => $this->t('How many users would you like to generate?'), '#default_value' => $this->getSetting('num'), '#required' => TRUE, '#min' => 0);
$form['kill'] = array('#type' => 'checkbox', '#title' => $this->t('Delete all users (except user id 1) before generating new users.'), '#default_value' => $this->getSetting('kill'));
$options = user_role_names(TRUE);
unset($options[DRUPAL_AUTHENTICATED_RID]);
$form['roles'] = array('#type' => 'checkboxes', '#title' => $this->t('Which roles should the users receive?'), '#description' => $this->t('Users always receive the <em>authenticated user</em> role.'), '#options' => $options);
$form['pass'] = array('#type' => 'textfield', '#title' => $this->t('Password to be set'), '#default_value' => $this->getSetting('pass'), '#size' => 32, '#description' => $this->t('Leave this field empty if you do not need to set a password'));
$options = array(1 => $this->t('Now'));
foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) {
$options[$interval] = $this->dateFormatter->formatInterval($interval, 1) . ' ' . $this->t('ago');
}
$form['time_range'] = array('#type' => 'select', '#title' => $this->t('How old should user accounts be?'), '#description' => $this->t('User ages will be distributed randomly from the current time, back to the selected time.'), '#options' => $options, '#default_value' => 604800);
return $form;
}
示例11: testBuildRow
/**
* @covers ::buildRow
*
* @dataProvider providerTestBuildRow
*
* @depends testBuildOperations
*/
function testBuildRow($payment_currency_exists)
{
$payment_changed_time = time();
$payment_changed_time_formatted = $this->randomMachineName();
$payment_currency_code = $this->randomMachineName();
$payment_amount = mt_rand();
$payment_amount_formatted = $this->randomMachineName();
$payment_status_definition = array('label' => $this->randomMachineName());
$payment_status = $this->getMock(PaymentStatusInterface::class);
$payment_status->expects($this->any())->method('getPluginDefinition')->willReturn($payment_status_definition);
$owner = $this->getMock(UserInterface::class);
$payment_method_label = $this->randomMachineName();
$payment_method_definition = ['label' => $payment_method_label];
$payment_method = $this->getMock(PaymentMethodInterface::class);
$payment_method->expects($this->atLeastOnce())->method('getPluginDefinition')->willReturn($payment_method_definition);
$payment = $this->getMock(PaymentInterface::class);
$payment->expects($this->any())->method('getAmount')->willReturn($payment_amount);
$payment->expects($this->any())->method('getChangedTime')->willReturn($payment_changed_time);
$payment->expects($this->any())->method('getCurrencyCode')->willReturn($payment_currency_code);
$payment->expects($this->any())->method('getOwner')->willReturn($owner);
$payment->expects($this->any())->method('getPaymentMethod')->willReturn($payment_method);
$payment->expects($this->any())->method('getPaymentStatus')->willReturn($payment_status);
$currency = $this->getMock(CurrencyInterface::class);
$currency->expects($this->once())->method('formatAmount')->with($payment_amount)->willReturn($payment_amount_formatted);
$map = array(array($payment_currency_code, $payment_currency_exists ? $currency : NULL), array('XXX', $payment_currency_exists ? NULL : $currency));
$this->currencyStorage->expects($this->atLeastOnce())->method('load')->willReturnMap($map);
$this->dateFormatter->expects($this->once())->method('format')->with($payment_changed_time)->willReturn($payment_changed_time_formatted);
$this->moduleHandler->expects($this->any())->method('invokeAll')->willReturn([]);
$build = $this->sut->buildRow($payment);
unset($build['data']['operations']['data']['#attached']);
$expected_build = array('data' => array('updated' => $payment_changed_time_formatted, 'status' => $payment_status_definition['label'], 'amount' => $payment_amount_formatted, 'payment_method' => $payment_method_label, 'owner' => array('data' => array('#theme' => 'username', '#account' => $owner)), 'operations' => array('data' => array('#type' => 'operations', '#links' => []))));
$this->assertSame($expected_build, $build);
}
示例12: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$index = $this->entity;
// Do not allow the form to be cached. See
// \Drupal\views_ui\ViewEditForm::form().
$form_state->disableCache();
if ($index instanceof UnsavedConfigurationInterface && $index->hasChanges()) {
if ($index->isLocked()) {
$form['#disabled'] = TRUE;
$username = array('#theme' => 'username', '#account' => $index->getLockOwner($this->entityTypeManager));
$lock_message_substitutions = array('@user' => $this->getRenderer()->render($username), '@age' => $this->dateFormatter->formatTimeDiffSince($index->getLastUpdated()), ':url' => $index->toUrl('break-lock-form')->toString());
$form['locked'] = array('#type' => 'container', '#attributes' => array('class' => array('index-locked', 'messages', 'messages--warning')), '#children' => $this->t('This index is being edited by user @user, and is therefore locked from editing by others. This lock is @age old. Click here to <a href=":url">break this lock</a>.', $lock_message_substitutions), '#weight' => -10);
} else {
$form['changed'] = array('#type' => 'container', '#attributes' => array('class' => array('index-changed', 'messages', 'messages--warning')), '#children' => $this->t('You have unsaved changes.'), '#weight' => -10);
}
}
// Set an appropriate page title.
$form['#title'] = $this->t('Manage fields for search index %label', array('%label' => $index->label()));
$form['#tree'] = TRUE;
$form['description']['#markup'] = $this->t('<p>The data type of a field determines how it can be used for searching and filtering. The boost is used to give additional weight to certain fields, e.g. titles or tags.</p> <p>Whether detailed field types are supported depends on the type of server this index resides on. In any case, fields of type "Fulltext" will always be fulltext-searchable.</p>');
if ($index->hasValidServer()) {
$form['description']['#markup'] .= '<p>' . $this->t('Check the <a href=":server-url">' . "server's</a> backend class description for details.", array(':server-url' => $index->getServerInstance()->toUrl('canonical')->toString())) . '</p>';
}
if ($fields = $index->getFieldsByDatasource(NULL)) {
$form['_general'] = $this->buildFieldsTable($fields);
$form['_general']['#title'] = $this->t('General');
}
foreach ($index->getDatasources() as $datasource_id => $datasource) {
$fields = $index->getFieldsByDatasource($datasource_id);
$form[$datasource_id] = $this->buildFieldsTable($fields);
$form[$datasource_id]['#title'] = $datasource->label();
}
$form['actions'] = $this->actionsElement($form, $form_state);
return $form;
}
示例13: settingsForm
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state)
{
$bundles = $this->mediaBundleStorage->loadMultiple();
if (empty($bundles)) {
$create_url = $this->urlGenerator->generateFromRoute('media.bundle_add');
$this->setMessage($this->t('You do not have any media bundles that can be generated. <a href="@create-bundle">Go create a new media bundle</a>', ['@create-bundle' => $create_url]), 'error', FALSE);
return [];
}
$options = [];
foreach ($bundles as $bundle) {
$options[$bundle->id()] = ['bundle' => ['#markup' => $bundle->label()]];
}
$form['media_bundles'] = ['#type' => 'tableselect', '#header' => ['bundle' => $this->t('Media bundle')], '#options' => $options];
$form['kill'] = ['#type' => 'checkbox', '#title' => $this->t('<strong>Delete all media</strong> in these bundles before generating new media.'), '#default_value' => $this->getSetting('kill')];
$form['num'] = ['#type' => 'number', '#title' => $this->t('How many media items would you like to generate?'), '#default_value' => $this->getSetting('num'), '#required' => TRUE, '#min' => 0];
$options = [1 => $this->t('Now')];
foreach ([3600, 86400, 604800, 2592000, 31536000] as $interval) {
$options[$interval] = $this->dateFormatter->formatInterval($interval, 1) . ' ' . $this->t('ago');
}
$form['time_range'] = ['#type' => 'select', '#title' => $this->t('How far back in time should the media be dated?'), '#description' => $this->t('Media creation dates will be distributed randomly from the current time, back to the selected time.'), '#options' => $options, '#default_value' => 604800];
$form['name_length'] = ['#type' => 'number', '#title' => $this->t('Maximum number of words in names'), '#default_value' => $this->getSetting('name_length'), '#required' => TRUE, '#min' => 1, '#max' => 255];
$options = [];
// We always need a language.
$languages = $this->languageManager->getLanguages(LanguageInterface::STATE_ALL);
foreach ($languages as $langcode => $language) {
$options[$langcode] = $language->getName();
}
$form['add_language'] = ['#type' => 'select', '#title' => $this->t('Set language on media'), '#multiple' => TRUE, '#description' => $this->t('Requires locale.module'), '#options' => $options, '#default_value' => [$this->languageManager->getDefaultLanguage()->getId()]];
$form['#redirect'] = FALSE;
return $form;
}
示例14: index
/**
* Index.
*
* @return array
* Render array with all the entries.
*/
public function index()
{
$output = ['#cache' => ['max-age' => 0]];
// This is going to be reused at two places: once for the TableSortExtender,
// and once for the table header itself.
$header = [['data' => $this->t('Created'), 'field' => 'p.created'], ['data' => $this->t('Changed'), 'field' => 'p.changed'], ['data' => $this->t('Name'), 'field' => 'p.name'], ['data' => $this->t('Phone'), 'field' => 'p.phone'], ['data' => $this->t('Operations'), 'colspan' => '2']];
$query = $this->connection->select('phonebook', 'p')->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender');
$query->fields('p');
$result = $query->orderByHeader($header)->limit(25)->execute();
$output['table'] = ['#type' => 'table', '#header' => $header, '#empty' => $this->t('No entries found.')];
foreach ($result as $row) {
$output['table'][] = [['data' => ['#markup' => $this->date_formatter->format($row->created)]], ['data' => ['#markup' => $this->date_formatter->formatTimeDiffSince($row->changed)]], ['data' => ['#markup' => $row->name]], ['data' => ['#markup' => $row->phone]], ['data' => ['#markup' => $this->l($this->t('edit'), new Url('d8phonebook.edit', ['phonebook' => $row->pbid]))]], ['data' => ['#markup' => $this->l($this->t('delete'), new Url('d8phonebook.delete', ['phonebook' => $row->pbid], ['query' => ['token' => $this->csrf_token_generator->get('phonebook/' . $row->pbid . '/delete')]]))]]];
}
$output['pager'] = array('#type' => 'pager');
return $output;
}
示例15: adminOverview
/**
* Displays the aggregator administration page.
*
* @return array
* A render array as expected by drupal_render().
*/
public function adminOverview()
{
$entity_manager = $this->entityManager();
$feeds = $entity_manager->getStorage('aggregator_feed')->loadMultiple();
$header = array($this->t('Title'), $this->t('Items'), $this->t('Last update'), $this->t('Next update'), $this->t('Operations'));
$rows = array();
/** @var \Drupal\aggregator\FeedInterface[] $feeds */
foreach ($feeds as $feed) {
$row = array();
$row[] = $feed->link();
$row[] = $this->formatPlural($entity_manager->getStorage('aggregator_item')->getItemCount($feed), '1 item', '@count items');
$last_checked = $feed->getLastCheckedTime();
$refresh_rate = $feed->getRefreshRate();
$row[] = $last_checked ? $this->t('@time ago', array('@time' => $this->dateFormatter->formatInterval(REQUEST_TIME - $last_checked))) : $this->t('never');
if (!$last_checked && $refresh_rate) {
$next_update = $this->t('imminently');
} elseif ($last_checked && $refresh_rate) {
$next_update = $next = $this->t('%time left', array('%time' => $this->dateFormatter->formatInterval($last_checked + $refresh_rate - REQUEST_TIME)));
} else {
$next_update = $this->t('never');
}
$row[] = $next_update;
$links['edit'] = ['title' => $this->t('Edit'), 'url' => Url::fromRoute('entity.aggregator_feed.edit_form', ['aggregator_feed' => $feed->id()])];
$links['delete'] = array('title' => $this->t('Delete'), 'url' => Url::fromRoute('entity.aggregator_feed.delete_form', ['aggregator_feed' => $feed->id()]));
$links['delete_items'] = array('title' => $this->t('Delete items'), 'url' => Url::fromRoute('aggregator.feed_items_delete', ['aggregator_feed' => $feed->id()]));
$links['update'] = array('title' => $this->t('Update items'), 'url' => Url::fromRoute('aggregator.feed_refresh', ['aggregator_feed' => $feed->id()]));
$row[] = array('data' => array('#type' => 'operations', '#links' => $links));
$rows[] = $row;
}
$build['feeds'] = array('#prefix' => '<h3>' . $this->t('Feed overview') . '</h3>', '#type' => 'table', '#header' => $header, '#rows' => $rows, '#empty' => $this->t('No feeds available. <a href="@link">Add feed</a>.', array('@link' => $this->url('aggregator.feed_add'))));
return $build;
}