/** * {@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); } } $args['%index'] = $index->label(); $form['#title'] = $this->t('Add fields to index %index', $args); $form['properties'] = array('#theme' => 'search_api_form_item_list'); $datasources = array('' => NULL); $datasources += $this->entity->getDatasources(); foreach ($datasources as $datasource) { $form['properties'][] = $this->getDatasourceListItem($datasource); } // Log any unmapped types that were encountered. if ($this->unmappedFields) { $unmapped_types = array(); foreach ($this->unmappedFields as $type => $fields) { $unmapped_types[] = implode(', ', $fields) . ' (' . new FormattableMarkup('type @type', array('@type' => $type)) . ')'; } $vars['@fields'] = implode('; ', $unmapped_types); $vars['%index'] = $this->entity->label(); \Drupal::logger('search_api')->warning('Warning while retrieving available fields for index %index: could not find a type mapping for the following fields: @fields.', $vars); } $form['actions'] = $this->actionsElement($form, $form_state); return $form; }
/** * 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->formatTimeDiffSince($last_checked))) : $this->t('never'); $row[] = $last_checked && $refresh_rate ? $this->t('@time left', array('@time' => $this->dateFormatter->formatTimeDiffUntil($last_checked + $refresh_rate))) : $this->t('never'); $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; }
/** * {@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; }
/** * 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; }
/** * Formats a timestamp. * * @param int $timestamp * A UNIX timestamp to format. * * @return string * The formatted timestamp string using the past or future format setting. */ protected function formatTimestamp($timestamp) { $granularity = $this->getSetting('granularity'); $options = ['granularity' => $granularity]; if ($this->request->server->get('REQUEST_TIME') > $timestamp) { return SafeMarkup::format($this->getSetting('past_format'), ['@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, $options)]); } else { return SafeMarkup::format($this->getSetting('future_format'), ['@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, $options)]); } }
/** * {@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('::submitCron')); $status = '<p>' . $this->t('Last run: %time ago.', array('%time' => $this->dateFormatter->formatTimeDiffSince($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' => $this->url('system.cron', array('key' => $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' => 'https://www.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); }
/** * Tests the formatTimeDiffUntil method. * * @covers ::formatTimeDiffUntil */ public function testFormatTimeDiffUntil() { $expected = '1 second'; $request_time = $this->createTimestamp('2013-12-11 10:09:08'); $timestamp = $this->createTimestamp('2013-12-11 10:09:09'); $options = array(); // Mocks the formatDiff function of the dateformatter object. $this->dateFormatterStub->expects($this->any())->method('formatDiff')->with($timestamp, $request_time, $options)->will($this->returnValue($expected)); $request = Request::createFromGlobals(); $request->server->set('REQUEST_TIME', $request_time); // Mocks a the request stack getting the current request. $this->requestStack->expects($this->any())->method('getCurrentRequest')->willReturn($request); $this->assertEquals($expected, $this->dateFormatterStub->formatTimeDiffSince($timestamp, $options)); }
/** * {@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\\SafeMarkup::checkPlain', user_role_names(TRUE)); unset($roles[RoleInterface::AUTHENTICATED_ID]); $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->formatTimeDiffSince($entity->getCreatedTime()); $row['access'] = $entity->access ? $this->t('@time ago', array('@time' => $this->dateFormatter->formatTimeDiffSince($entity->getLastAccessedTime()))) : t('never'); return $row + parent::buildRow($entity); }
/** * {@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->formatTimeDiffSince($value, array('granularity' => is_numeric($custom_format) ? $custom_format : 2)); case 'time ago': return $this->t('%time ago', array('%time' => $this->dateFormatter->formatTimeDiffSince($value, array('granularity' => is_numeric($custom_format) ? $custom_format : 2)))); case 'raw time hence': return $this->dateFormatter->formatTimeDiffUntil($value, array('granularity' => is_numeric($custom_format) ? $custom_format : 2)); case 'time hence': return $this->t('%time hence', array('%time' => $this->dateFormatter->formatTimeDiffUntil($value, array('granularity' => is_numeric($custom_format) ? $custom_format : 2)))); case 'raw time span': return ($time_diff < 0 ? '-' : '') . $this->dateFormatter->formatTimeDiffSince($value, array('strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2)); case 'inverse time span': return ($time_diff > 0 ? '-' : '') . $this->dateFormatter->formatTimeDiffSince($value, array('strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2)); case 'time span': $time = $this->dateFormatter->formatTimeDiffSince($value, array('strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2)); return $time_diff < 0 ? $this->t('%time hence', array('%time' => $time)) : $this->t('%time ago', array('%time' => $time)); 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, FormStateInterface $form_state) { $view = $this->entity; $display_id = $this->displayID; // Do not allow the form to be cached, because $form_state->get('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->get('view') to $form_state->getTemporary()['view']. // - Add a #process function to initialize $form_state->getTemporary()['view'] // on cached form submissions. // - Use \Drupal\Core\Form\FormStateInterface::loadInclude(). $form_state->disableCache(); 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'; $form['#attached']['library'][] = 'views_ui/views_ui.admin'; $form['#attached']['library'][] = 'views_ui/admin.styling'; $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' => $this->entityManager->getStorage('user')->load($view->lock->owner)); $lock_message_substitutions = array('!user' => drupal_render($username), '!age' => $this->dateFormatter->formatTimeDiffSince($view->lock->updated), '@url' => $view->url('break-lock-form')); $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="@url">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->set('display_id', $display_id); // The part of the page where editing will take place. $form['displays']['settings'] = array('#type' => 'container', '#id' => 'edit-display-settings', '#attributes' => array('class' => array('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); } return $form; }