/** * {@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->dateService->format(REQUEST_TIME, $entity->id()); return $row + parent::buildRow($entity); }
/** * {@inheritdoc} */ public function buildForm(array $form, array &$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); }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, array &$form_state) { $processors = $this->configuration['processors']; $info = $this->getPluginDefinition(); $counts = array(3, 5, 10, 15, 20, 25); $items = array_map(function ($count) { return $this->dateFormatter->formatInterval($count, '1 item', '@count items'); }, array_combine($counts, $counts)); $intervals = array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800); $period = array_map(array($this->dateFormatter, 'formatInterval'), array_combine($intervals, $intervals)); $period[AGGREGATOR_CLEAR_NEVER] = t('Never'); $form['processors'][$info['id']] = array(); // Only wrap into details if there is a basic configuration. if (isset($form['basic_conf'])) { $form['processors'][$info['id']] = array('#type' => 'details', '#title' => t('Default processor settings'), '#description' => $info['description'], '#open' => in_array($info['id'], $processors)); } $form['processors'][$info['id']]['aggregator_summary_items'] = array('#type' => 'select', '#title' => t('Number of items shown in listing pages'), '#default_value' => $this->configuration['source']['list_max'], '#empty_value' => 0, '#options' => $items); $form['processors'][$info['id']]['aggregator_clear'] = array('#type' => 'select', '#title' => t('Discard items older than'), '#default_value' => $this->configuration['items']['expire'], '#options' => $period, '#description' => t('Requires a correctly configured <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status')))); $lengths = array(0, 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000); $options = array_map(function ($length) { return $length == 0 ? t('Unlimited') : format_plural($length, '1 character', '@count characters'); }, array_combine($lengths, $lengths)); $form['processors'][$info['id']]['aggregator_teaser_length'] = array('#type' => 'select', '#title' => t('Length of trimmed description'), '#default_value' => $this->configuration['items']['teaser_length'], '#options' => $options, '#description' => t('The maximum number of characters used in the trimmed version of content.')); return $form; }
public function buildOptionsForm(&$form, &$form_state) { $date_formats = array(); foreach ($this->dateFormatStorage->loadMultiple() as $machine_name => $value) { $date_formats[$machine_name] = t('@name format: @date', array('@name' => $value->label(), '@date' => $this->dateService->format(REQUEST_TIME, $machine_name))); } $form['date_format'] = array('#type' => 'select', '#title' => t('Date format'), '#options' => $date_formats + array('custom' => t('Custom'), 'raw time ago' => t('Time ago'), 'time ago' => t('Time ago (with "ago" appended)'), 'raw time hence' => t('Time hence'), 'time hence' => t('Time hence (with "hence" appended)'), 'raw time span' => t('Time span (future dates have "-" prepended)'), 'inverse time span' => t('Time span (past dates have "-" prepended)'), 'time span' => t('Time span (with "ago/hence" appended)')), '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small'); $form['custom_date_format'] = array('#type' => 'textfield', '#title' => t('Custom date format'), '#description' => t('If "Custom", see <a href="http://us.php.net/manual/en/function.date.php" target="_blank">the PHP docs</a> for date formats. Otherwise, enter the number of different time units to display, which defaults to 2.'), '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : ''); // Setup #states for all possible date_formats on the custom_date_format form element. foreach (array('custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span') as $custom_date_possible) { $form['custom_date_format']['#states']['visible'][] = array(':input[name="options[date_format]"]' => array('value' => $custom_date_possible)); } $form['timezone'] = array('#type' => 'select', '#title' => t('Timezone'), '#description' => t('Timezone to be used for date output.'), '#options' => array('' => t('- Default site/user timezone -')) + system_time_zones(FALSE), '#default_value' => $this->options['timezone']); foreach (array_merge(array('custom'), array_keys($date_formats)) as $timezone_date_formats) { $form['timezone']['#states']['visible'][] = array(':input[name="options[date_format]"]' => array('value' => $timezone_date_formats)); } parent::buildOptionsForm($form, $form_state); }
/** * Throws an exception if the current user triggers flood control. * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException */ protected function contactFloodControl() { $limit = $this->config('contact.settings')->get('flood.limit'); $interval = $this->config('contact.settings')->get('flood.interval'); if (!$this->flood->isAllowed('contact', $limit, $interval)) { drupal_set_message($this->t('You cannot send more than %limit messages in @interval. Try again later.', array('%limit' => $limit, '@interval' => $this->dateFormatter->formatInterval($interval))), 'error'); throw new AccessDeniedHttpException(); } }
/** * Form constructor for the comment overview administration form. * * @param array $form * An associative array containing the structure of the form. * @param array $form_state * An associative array containing the current state of the form. * @param string $type * The type of the overview form ('approval' or 'new'). * * @return array * The form structure. */ public function buildForm(array $form, array &$form_state, $type = 'new') { // Build an 'Update options' form. $form['options'] = array('#type' => 'details', '#title' => $this->t('Update options'), '#open' => TRUE, '#attributes' => array('class' => array('container-inline'))); if ($type == 'approval') { $options['publish'] = $this->t('Publish the selected comments'); } else { $options['unpublish'] = $this->t('Unpublish the selected comments'); } $options['delete'] = $this->t('Delete the selected comments'); $form['options']['operation'] = array('#type' => 'select', '#title' => $this->t('Action'), '#title_display' => 'invisible', '#options' => $options, '#default_value' => 'publish'); $form['options']['submit'] = array('#type' => 'submit', '#value' => $this->t('Update')); // Load the comments that need to be displayed. $status = $type == 'approval' ? CommentInterface::NOT_PUBLISHED : CommentInterface::PUBLISHED; $header = array('subject' => array('data' => $this->t('Subject'), 'specifier' => 'subject'), 'author' => array('data' => $this->t('Author'), 'specifier' => 'name', 'class' => array(RESPONSIVE_PRIORITY_MEDIUM)), 'posted_in' => array('data' => $this->t('Posted in'), 'class' => array(RESPONSIVE_PRIORITY_LOW)), 'changed' => array('data' => $this->t('Updated'), 'specifier' => 'changed', 'sort' => 'desc', 'class' => array(RESPONSIVE_PRIORITY_LOW)), 'operations' => $this->t('Operations')); $cids = $this->commentStorage->getQuery()->condition('status', $status)->tableSort($header)->pager(50)->execute(); /** @var $comments \Drupal\comment\CommentInterface[] */ $comments = $this->commentStorage->loadMultiple($cids); // Build a table listing the appropriate comments. $options = array(); $destination = drupal_get_destination(); $commented_entity_ids = array(); $commented_entities = array(); foreach ($comments as $comment) { $commented_entity_ids[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId(); } foreach ($commented_entity_ids as $entity_type => $ids) { $commented_entities[$entity_type] = $this->entityManager->getStorage($entity_type)->loadMultiple($ids); } foreach ($comments as $comment) { /** @var $commented_entity \Drupal\Core\Entity\EntityInterface */ $commented_entity = $commented_entities[$comment->getCommentedEntityTypeId()][$comment->getCommentedEntityId()]; $username = array('#theme' => 'username', '#account' => comment_prepare_author($comment)); $body = ''; if (!empty($comment->comment_body->value)) { $body = $comment->comment_body->value; } $comment_permalink = $comment->permalink(); $attributes = $comment_permalink->getOption('attributes') ?: array(); $attributes += array('title' => Unicode::truncate($body, 128)); $comment_permalink->setOption('attributes', $attributes); $options[$comment->id()] = array('title' => array('data' => array('#title' => $comment->getSubject() ?: $comment->id())), 'subject' => array('data' => array('#type' => 'link', '#title' => $comment->getSubject()) + $comment_permalink->toRenderArray()), 'author' => drupal_render($username), 'posted_in' => array('data' => array('#type' => 'link', '#title' => $commented_entity->label(), '#access' => $commented_entity->access('view')) + $commented_entity->urlInfo()->toRenderArray()), 'changed' => $this->date->format($comment->getChangedTime(), 'short')); $comment_uri_options = $comment->urlInfo()->getOptions(); $links = array(); $links['edit'] = array('title' => $this->t('Edit'), 'route_name' => 'comment.edit_page', 'route_parameters' => array('comment' => $comment->id()), 'options' => $comment_uri_options, 'query' => $destination); if ($this->moduleHandler->invoke('content_translation', 'translate_access', array($comment))) { $links['translate'] = array('title' => $this->t('Translate'), 'route_name' => 'content_translation.translation_overview_comment', 'route_parameters' => array('comment' => $comment->id()), 'options' => $comment_uri_options, 'query' => $destination); } $options[$comment->id()]['operations']['data'] = array('#type' => 'operations', '#links' => $links); } $form['comments'] = array('#type' => 'tableselect', '#header' => $header, '#options' => $options, '#empty' => $this->t('No comments available.')); $form['pager'] = array('#theme' => 'pager'); return $form; }
/** * {@inheritdoc} */ public function settingsForm(array $form, array &$form_state) { $time = new DrupalDateTime(); $format_types = $this->dateStorage->loadMultiple(); foreach ($format_types as $type => $type_info) { $format = $this->dateFormatter->format($time->format('U'), $type); $options[$type] = $type_info->label() . ' (' . $format . ')'; } $elements['format_type'] = array('#type' => 'select', '#title' => t('Date format'), '#description' => t("Choose a format for displaying the date. Be sure to set a format appropriate for the field, i.e. omitting time for a field that only has a date."), '#options' => $options, '#default_value' => $this->getSetting('format_type')); return $elements; }
/** * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { /** @var \Drupal\node\NodeInterface $entity */ $mark = array('#theme' => 'mark', '#mark_type' => node_mark($entity->id(), $entity->getChangedTime())); $langcode = $entity->language()->id; $uri = $entity->urlInfo(); $options = $uri->getOptions(); $options += $langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED && isset($languages[$langcode]) ? array('language' => $languages[$langcode]) : array(); $uri->setOptions($options); $row['title']['data'] = array('#type' => 'link', '#title' => $entity->label(), '#suffix' => ' ' . drupal_render($mark)) + $uri->toRenderArray(); $row['type'] = String::checkPlain(node_get_type_label($entity)); $row['author']['data'] = array('#theme' => 'username', '#account' => $entity->getOwner()); $row['status'] = $entity->isPublished() ? $this->t('published') : $this->t('not published'); $row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short'); $language_manager = \Drupal::languageManager(); if ($language_manager->isMultilingual()) { $row['language_name'] = $language_manager->getLanguageName($langcode); } $row['operations']['data'] = $this->buildOperations($entity); return $row + parent::buildRow($entity); }
/** * Displays details about a specific database log message. * * @param int $event_id * Unique ID of the database log message. * * @return array * If the ID is located in the Database Logging table, a build array in the * format expected by drupal_render(); * */ public function eventDetails($event_id) { $build = array(); if ($dblog = $this->database->query('SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid WHERE w.wid = :id', array(':id' => $event_id))->fetchObject()) { $severity = watchdog_severity_levels(); $message = $this->formatMessage($dblog); $username = array('#theme' => 'username', '#account' => user_load($dblog->uid)); $rows = array(array(array('data' => $this->t('Type'), 'header' => TRUE), $this->t($dblog->type)), array(array('data' => $this->t('Date'), 'header' => TRUE), $this->dateFormatter->format($dblog->timestamp, 'long')), array(array('data' => $this->t('User'), 'header' => TRUE), array('data' => $username)), array(array('data' => $this->t('Location'), 'header' => TRUE), l($dblog->location, $dblog->location)), array(array('data' => $this->t('Referrer'), 'header' => TRUE), l($dblog->referer, $dblog->referer)), array(array('data' => $this->t('Message'), 'header' => TRUE), $message), array(array('data' => $this->t('Severity'), 'header' => TRUE), $severity[$dblog->severity]), array(array('data' => $this->t('Hostname'), 'header' => TRUE), String::checkPlain($dblog->hostname)), array(array('data' => $this->t('Operations'), 'header' => TRUE), $dblog->link)); $build['dblog_table'] = array('#type' => 'table', '#rows' => $rows, '#attributes' => array('class' => array('dblog-event')), '#attached' => array('library' => array('dblog/drupal.dblog'))); } return $build; }
/** * Returns the user password reset page. * * @param int $uid * UID of user requesting reset. * @param int $timestamp * The current timestamp. * @param string $hash * Login link hash. * * @return array|\Symfony\Component\HttpFoundation\RedirectResponse * The form structure or a redirect response. * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException * If the login link is for a blocked user or invalid user ID. */ public function resetPass($uid, $timestamp, $hash) { $account = $this->currentUser(); $config = $this->config('user.settings'); // When processing the one-time login link, we have to make sure that a user // isn't already logged in. if ($account->isAuthenticated()) { // The current user is already logged in. if ($account->id() == $uid) { drupal_set_message($this->t('You are logged in as %user. <a href="!user_edit">Change your password.</a>', array('%user' => $account->getUsername(), '!user_edit' => $this->url('user.edit', array('user' => $account->id()))))); } else { if ($reset_link_user = $this->userStorage->load($uid)) { drupal_set_message($this->t('Another user (%other_user) is already logged into the site on this computer, but you tried to use a one-time link for user %resetting_user. Please <a href="!logout">logout</a> and try using the link again.', array('%other_user' => $account->getUsername(), '%resetting_user' => $reset_link_user->getUsername(), '!logout' => $this->url('user.logout')))); } else { // Invalid one-time link specifies an unknown user. drupal_set_message($this->t('The one-time login link you clicked is invalid.')); } } return $this->redirect('<front>'); } else { // The current user is not logged in, so check the parameters. // Time out, in seconds, until login URL expires. $timeout = $config->get('password_reset_timeout'); $current = REQUEST_TIME; /* @var \Drupal\user\UserInterface $user */ $user = $this->userStorage->load($uid); // Verify that the user exists and is active. if ($user && $user->isActive()) { // No time out for first time login. if ($user->getLastLoginTime() && $current - $timestamp > $timeout) { drupal_set_message($this->t('You have tried to use a one-time login link that has expired. Please request a new one using the form below.')); return $this->redirect('user.pass'); } elseif ($user->isAuthenticated() && $timestamp >= $user->getLastLoginTime() && $timestamp <= $current && $hash === user_pass_rehash($user->getPassword(), $timestamp, $user->getLastLoginTime())) { $expiration_date = $user->getLastLoginTime() ? $this->dateFormatter->format($timestamp + $timeout) : NULL; return $this->formBuilder()->getForm('Drupal\\user\\Form\\UserPasswordResetForm', $user, $expiration_date, $timestamp, $hash); } else { drupal_set_message($this->t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.')); return $this->redirect('user.pass'); } } } // Blocked or invalid user ID, so deny access. The parameters will be in the // watchdog's URL for the administrator to check. throw new AccessDeniedHttpException(); }
/** * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { $row['username']['data'] = array('#theme' => 'username', '#account' => $entity); $row['status'] = $entity->isActive() ? $this->t('active') : $this->t('blocked'); $roles = array_map('\\Drupal\\Component\\Utility\\String::checkPlain', user_role_names(TRUE)); unset($roles[DRUPAL_AUTHENTICATED_RID]); $users_roles = array(); foreach ($entity->getRoles() as $role) { if (isset($roles[$role])) { $users_roles[] = $roles[$role]; } } asort($users_roles); $row['roles']['data'] = array('#theme' => 'item_list', '#items' => $users_roles); $row['member_for'] = $this->dateFormatter->formatInterval(REQUEST_TIME - $entity->getCreatedTime()); $row['access'] = $entity->access ? $this->t('@time ago', array('@time' => $this->dateFormatter->formatInterval(REQUEST_TIME - $entity->getLastAccessedTime()))) : t('never'); return $row + parent::buildRow($entity); }
/** * Generates an overview table of older revisions of a node. * * @param \Drupal\node\NodeInterface $node * A node object. * * @return array * An array as expected by drupal_render(). */ public function revisionOverview(NodeInterface $node) { $account = $this->currentUser(); $node_storage = $this->entityManager()->getStorage('node'); $type = $node->getType(); $build = array(); $build['#title'] = $this->t('Revisions for %title', array('%title' => $node->label())); $header = array($this->t('Revision'), $this->t('Operations')); $revert_permission = ($account->hasPermission("revert {$type} revisions") || $account->hasPermission('revert all revisions') || $account->hasPermission('administer nodes')) && $node->access('update'); $delete_permission = ($account->hasPermission("delete {$type} revisions") || $account->hasPermission('delete all revisions') || $account->hasPermission('administer nodes')) && $node->access('delete'); $rows = array(); $vids = $node_storage->revisionIds($node); foreach (array_reverse($vids) as $vid) { if ($revision = $node_storage->loadRevision($vid)) { $row = array(); $revision_author = $revision->uid->entity; if ($vid == $node->getRevisionId()) { $username = array('#theme' => 'username', '#account' => $revision_author); $row[] = array('data' => $this->t('!date by !username', array('!date' => $this->l($this->date->format($revision->revision_timestamp->value, 'short'), 'node.view', array('node' => $node->id())), '!username' => drupal_render($username))) . ($revision->revision_log->value != '' ? '<p class="revision-log">' . Xss::filter($revision->revision_log->value) . '</p>' : ''), 'class' => array('revision-current')); $row[] = array('data' => String::placeholder($this->t('current revision')), 'class' => array('revision-current')); } else { $username = array('#theme' => 'username', '#account' => $revision_author); $row[] = $this->t('!date by !username', array('!date' => $this->l($this->date->format($revision->revision_timestamp->value, 'short'), 'node.revision_show', array('node' => $node->id(), 'node_revision' => $vid)), '!username' => drupal_render($username))) . ($revision->revision_log->value != '' ? '<p class="revision-log">' . Xss::filter($revision->revision_log->value) . '</p>' : ''); if ($revert_permission) { $links['revert'] = array('title' => $this->t('Revert'), 'route_name' => 'node.revision_revert_confirm', 'route_parameters' => array('node' => $node->id(), 'node_revision' => $vid)); } if ($delete_permission) { $links['delete'] = array('title' => $this->t('Delete'), 'route_name' => 'node.revision_delete_confirm', 'route_parameters' => array('node' => $node->id(), 'node_revision' => $vid)); } $row[] = array('data' => array('#type' => 'operations', '#links' => $links)); } $rows[] = $row; } } $build['node_revisions_table'] = array('#theme' => 'table', '#rows' => $rows, '#header' => $header, '#attached' => array('library' => array('node/drupal.node.admin'))); return $build; }
/** * 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(); foreach ($feeds as $feed) { $row = array(); $row[] = l($feed->label(), "aggregator/sources/" . $feed->id()); $row[] = $this->dateFormatter->formatInterval($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'); $row[] = $last_checked && $refresh_rate ? $this->t('%time left', array('%time' => $this->dateFormatter->formatInterval($last_checked + $refresh_rate - REQUEST_TIME))) : $this->t('never'); $links['edit'] = array('title' => $this->t('Edit'), 'route_name' => 'aggregator.feed_configure', 'route_parameters' => array('aggregator_feed' => $feed->id())); $links['delete'] = array('title' => $this->t('Delete'), 'route_name' => 'aggregator.feed_delete', 'route_parameters' => array('aggregator_feed' => $feed->id())); $links['delete_items'] = array('title' => $this->t('Delete items'), 'route_name' => 'aggregator.feed_items_delete', 'route_parameters' => array('aggregator_feed' => $feed->id())); $links['update'] = array('title' => $this->t('Update items'), 'route_name' => 'aggregator.feed_refresh', 'route_parameters' => array('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; }
/** * {@inheritdoc} */ public function render(ResultRow $values) { $value = $this->getValue($values); $format = $this->options['date_format']; if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span'))) { $custom_format = $this->options['custom_date_format']; } if ($value) { $timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL; $time_diff = REQUEST_TIME - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence) switch ($format) { case 'raw time ago': return $this->dateFormatter->formatInterval($time_diff, is_numeric($custom_format) ? $custom_format : 2); case 'time ago': return t('%time ago', array('%time' => $this->dateFormatter->formatInterval($time_diff, is_numeric($custom_format) ? $custom_format : 2))); case 'raw time hence': return $this->dateFormatter->formatInterval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2); case 'time hence': return t('%time hence', array('%time' => $this->dateFormatter->formatInterval(-$time_diff, is_numeric($custom_format) ? $custom_format : 2))); case 'raw time span': return ($time_diff < 0 ? '-' : '') . $this->dateFormatter->formatInterval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2); case 'inverse time span': return ($time_diff > 0 ? '-' : '') . $this->dateFormatter->formatInterval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2); case 'time span': return t($time_diff < 0 ? '%time hence' : '%time ago', array('%time' => $this->dateFormatter->formatInterval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2))); case 'custom': if ($custom_format == 'r') { return format_date($value, $format, $custom_format, $timezone, 'en'); } return format_date($value, $format, $custom_format, $timezone); default: return format_date($value, $format, '', $timezone); } } }
/** * {@inheritdoc} */ public function form(array $form, array &$form_state) { $view = $this->entity; $display_id = $this->displayID; // Do not allow the form to be cached, because $form_state['view'] can become // stale between page requests. // See views_ui_ajax_get_form() for how this affects #ajax. // @todo To remove this and allow the form to be cacheable: // - Change $form_state['view'] to $form_state['temporary']['view']. // - Add a #process function to initialize $form_state['temporary']['view'] // on cached form submissions. // - Use form_load_include(). $form_state['no_cache'] = TRUE; if ($display_id) { if (!$view->getExecutable()->setDisplay($display_id)) { $form['#markup'] = $this->t('Invalid display id @display', array('@display' => $display_id)); return $form; } } $form['#tree'] = TRUE; $form['#attached']['library'][] = 'core/jquery.ui.tabs'; $form['#attached']['library'][] = 'core/jquery.ui.dialog'; $form['#attached']['library'][] = 'core/drupal.states'; $form['#attached']['library'][] = 'core/drupal.tabledrag'; if (!\Drupal::config('views.settings')->get('no_javascript')) { $form['#attached']['library'][] = 'views_ui/views_ui.admin'; } $form['#attached']['css'] = static::getAdminCSS(); $form['#attached']['js'][] = array('data' => array('views' => array('ajax' => array('id' => '#views-ajax-body', 'title' => '#views-ajax-title', 'popup' => '#views-ajax-popup', 'defaultForm' => $view->getDefaultAJAXMessage()))), 'type' => 'setting'); $form += array('#prefix' => '', '#suffix' => ''); $view_status = $view->status() ? 'enabled' : 'disabled'; $form['#prefix'] .= '<div class="views-edit-view views-admin ' . $view_status . ' clearfix">'; $form['#suffix'] = '</div>' . $form['#suffix']; $form['#attributes']['class'] = array('form-edit'); if ($view->isLocked()) { $username = array('#theme' => 'username', '#account' => user_load($view->lock->owner)); $lock_message_substitutions = array('!user' => drupal_render($username), '!age' => $this->dateFormatter->formatInterval(REQUEST_TIME - $view->lock->updated), '!break' => $view->url('break-lock')); $form['locked'] = array('#type' => 'container', '#attributes' => array('class' => array('view-locked', 'messages', 'messages--warning')), '#children' => $this->t('This view is being edited by user !user, and is therefore locked from editing by others. This lock is !age old. Click here to <a href="!break">break this lock</a>.', $lock_message_substitutions), '#weight' => -10); } else { $form['changed'] = array('#type' => 'container', '#attributes' => array('class' => array('view-changed', 'messages', 'messages--warning')), '#children' => $this->t('You have unsaved changes.'), '#weight' => -10); if (empty($view->changed)) { $form['changed']['#attributes']['class'][] = 'js-hide'; } } $form['displays'] = array('#prefix' => '<h1 class="unit-title clearfix">' . $this->t('Displays') . '</h1>', '#type' => 'container', '#attributes' => array('class' => array('views-displays'))); $form['displays']['top'] = $this->renderDisplayTop($view); // The rest requires a display to be selected. if ($display_id) { $form_state['display_id'] = $display_id; // The part of the page where editing will take place. $form['displays']['settings'] = array('#type' => 'container', '#id' => 'edit-display-settings'); // Add a text that the display is disabled. if ($view->getExecutable()->displayHandlers->has($display_id)) { if (!$view->getExecutable()->displayHandlers->get($display_id)->isEnabled()) { $form['displays']['settings']['disabled']['#markup'] = $this->t('This display is disabled.'); } } // Add the edit display content $tab_content = $this->getDisplayTab($view); $tab_content['#theme_wrappers'] = array('container'); $tab_content['#attributes'] = array('class' => array('views-display-tab')); $tab_content['#id'] = 'views-tab-' . $display_id; // Mark deleted displays as such. $display = $view->get('display'); if (!empty($display[$display_id]['deleted'])) { $tab_content['#attributes']['class'][] = 'views-display-deleted'; } // Mark disabled displays as such. if ($view->getExecutable()->displayHandlers->has($display_id) && !$view->getExecutable()->displayHandlers->get($display_id)->isEnabled()) { $tab_content['#attributes']['class'][] = 'views-display-disabled'; } $form['displays']['settings']['settings_content'] = array('#type' => 'container', 'tab_content' => $tab_content); // The content of the popup dialog. $form['ajax-area'] = array('#type' => 'container', '#id' => 'views-ajax-popup'); $form['ajax-area']['ajax-title'] = array('#markup' => '<div id="views-ajax-title"></div>'); $form['ajax-area']['ajax-body'] = array('#type' => 'container', '#id' => 'views-ajax-body', '#children' => $view->getDefaultAJAXMessage()); } return $form; }
/** * {@inheritdoc} */ public function render(ResultRow $values) { $value = $values->{$this->field_alias}; return $this->dateFormatter->formatInterval($value, isset($this->options['granularity']) ? $this->options['granularity'] : 2); }
public function summaryTitle() { $results_lifespan = $this->getLifespan('results'); $output_lifespan = $this->getLifespan('output'); return $this->dateFormatter->formatInterval($results_lifespan, 1) . '/' . $this->dateFormatter->formatInterval($output_lifespan, 1); }
/** * {@inheritdoc} */ public function getQuestion() { return t('Are you sure you want to remove the format %name : %format?', array('%name' => $this->entity->label(), '%format' => $this->dateFormatter->format(REQUEST_TIME, $this->entity->id()))); }
/** * Tests the formatInterval method for 0 second. */ public function testFormatIntervalZeroSecond() { $this->stringTranslation->expects($this->once())->method('translate')->with('0 sec', array(), array('langcode' => 'xxx-lolspeak'))->will($this->returnValue('0 sec')); $result = $this->date->formatInterval(0, 1, 'xxx-lolspeak'); $this->assertEquals('0 sec', $result); }