/**
  * Render a list of entries in the database.
  */
 public function listOfGroupings()
 {
     $content = array();
     $content['add_group'] = $this->formBuilder()->getForm('Drupal\\ea_groupings\\EAGroupingsAddForm');
     $content['message'] = array('#markup' => $this->t('Generate a list of all groupings on the site.'));
     $row = array();
     $rows = array();
     $header = array(array('data' => $this->t('ID'), 'field' => 'groupings.gid'), NULL, array('data' => $this->t('Title'), 'field' => 'groupings.title'), array('data' => $this->t('Parent')), $this->t('View'), $this->t('Edit'), $this->t('Delete'));
     foreach ($groupings = EAGroupingsStorage::loadSorted($header) as $grouping) {
         $row = array();
         $row['data']['gid'] = $grouping->gid;
         $row['data']['logo'] = NULL;
         if (!empty($grouping->logo_fid)) {
             $file = File::load($grouping->logo_fid);
             if ($file) {
                 $logo_url = ImageStyle::load('ea_groupings_50x50')->buildUrl($file->getFileUri());
                 $image_array = array('#theme' => 'image', '#uri' => $logo_url, '#alt' => $this->t('Logo for @grouping', array('@grouping' => $grouping->title)), '#title' => $this->t('@grouping', array('@grouping' => $grouping->title)));
                 $rendered_image = \Drupal::service('renderer')->render($image_array);
                 $row['data']['logo'] = $rendered_image;
             }
         }
         $row['data']['title'] = $grouping->title;
         $row['data']['parent_title'] = NULL;
         // Load parent grouping to get title.
         if (!empty($grouping->parent_gid)) {
             $parent_grouping_entries = EAGroupingsStorage::load(array('gid' => $grouping->parent_gid));
             if ($parent_grouping_entries) {
                 $parent_grouping = $parent_grouping_entries[0];
                 $row['data']['parent_title'] = $parent_grouping->title;
             }
         }
         // Format view link.
         $view_link_url = Url::fromRoute('ea_groupings.grouping', array('grouping' => $grouping->gid));
         $view_link = \Drupal::l($this->t('View'), $view_link_url);
         // Format edit link.
         $edit_link_url = Url::fromRoute('ea_groupings.edit', array('grouping' => $grouping->gid));
         $edit_link = \Drupal::l($this->t('Edit'), $edit_link_url);
         // Format delete link.
         $delete_link_url = Url::fromRoute('ea_groupings.delete', array('grouping' => $grouping->gid));
         $delete_link = \Drupal::l($this->t('Delete'), $delete_link_url);
         // Add links to row.
         $row['data']['view_link'] = $view_link;
         $row['data']['edit_link'] = $edit_link;
         $row['data']['delete_link'] = $delete_link;
         // Remove all unused variables.
         $rows[] = $row;
     }
     $content['table'] = array('#type' => 'table', '#header' => $header, '#rows' => $rows, '#empty' => t('No groupings available.'));
     $altered_table = \Drupal::moduleHandler()->invokeAll('ea_groupings_alter_table', array($content['table']));
     if (!empty($altered_table)) {
         $content['table'] = $altered_table;
     }
     $content['pager'] = array('#type' => 'pager');
     return $content;
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $values = $form_state->getValues();
     $entry = array('title' => $values['title'], 'description' => $values['description'], 'logo_fid' => $values['logo_fid'][0], 'time_zone' => $values['time_zone'], 'parent_gid' => empty($values['parent_gid']) ? NULL : $values['parent_gid']);
     // Save the submitted entry.
     $return = EAGroupingsStorage::insert($entry);
     if ($return) {
         // Add logo image file permanently.
         if (!empty($values['logo_fid'][0])) {
             $file = file_load($values['logo_fid'][0]);
             $file->status = FILE_STATUS_PERMANENT;
             $file->save();
             \Drupal::service('file.usage')->add($file, 'ea_groupings', 'group', $return);
         }
         drupal_set_message(t('Added @title', array('@title' => $entry['title'])));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $grouping = \Drupal::request()->get('grouping');
     // Delete the entry.
     EAGroupingsStorage::delete(array('gid' => $grouping->gid));
     // Set logo image file for removal.
     if ($grouping->logo_fid) {
         $file = file_load($grouping->logo_fid);
         if ($file) {
             \Drupal::service('file.usage')->delete($file, 'ea_groupings', 'group', $grouping->gid);
         }
     }
     // Set a message that the entity was deleted.
     drupal_set_message(t('Deleted @title', array('@title' => $grouping->title)));
     // Redirect the user to the list controller when complete.
     $form_state->setRedirectUrl($this->getCancelUrl());
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $values = $form_state->getValues();
     $entry = array('gid' => $values['gid'], 'title' => $values['title'], 'description' => $values['description'], 'time_zone' => $values['time_zone'], 'parent_gid' => empty($values['parent_gid']) ? NULL : $values['parent_gid']);
     // If logo fid is changed, delete old and set new for permanent status.
     $logo_fid = isset($values['logo_fid'][0]) ? $values['logo_fid'][0] : NULL;
     $old_logo_fid = $values['old_logo_fid'];
     if (!empty($logo_fid) && $logo_fid != $old_logo_fid) {
         // Set old file for removal.
         $file = file_load($old_logo_fid);
         if ($file) {
             \Drupal::service('file.usage')->delete($file, 'ea_groupings', 'group', $values['gid']);
         }
         // Add file permanently.
         $file = file_load($logo_fid);
         if ($file) {
             $file->status = FILE_STATUS_PERMANENT;
             $file->save();
             \Drupal::service('file.usage')->add($file, 'ea_groupings', 'group', $values['gid']);
         }
         // Add new fid to entry.
         $entry['logo_fid'] = $logo_fid;
     }
     // Save the submitted entry.
     $return = EAGroupingsStorage::update($entry);
     if ($return) {
         drupal_set_message(t('Updated @title', array('@title' => $entry['title'])));
     }
 }