/**
  * Sorts a structured array by either a set 'weight' property or by the ID.
  *
  * @param array $a
  *   First item for comparison.
  * @param array $b
  *   Second item for comparison.
  *
  * @return int
  *   The comparison result for uasort().
  */
 public static function sort(array $a, array $b)
 {
     if (isset($a['weight']) || isset($b['weight'])) {
         return SortArray::sortByWeightElement($a, $b);
     } else {
         return SortArray::sortByKeyString($a, $b, 'id');
     }
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function getDefinitions($sorted = TRUE)
 {
     $definitions = parent::getDefinitions();
     // Sort by the schema number (a.k.a. plugin ID).
     if ($sorted) {
         uasort($definitions, function ($a, $b) {
             return SortArray::sortByKeyInt($a, $b, 'id');
         });
     }
     return $definitions;
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)
 {
     $field_name = $this->fieldDefinition->getName();
     // Extract the values from $form_state->getValues().
     $path = array_merge($form['#parents'], array($field_name));
     $key_exists = NULL;
     $values = NestedArray::getValue($form_state->getValues(), $path, $key_exists);
     if ($key_exists) {
         // Account for drag-and-drop reordering if needed.
         if (!$this->handlesMultipleValues()) {
             // Remove the 'value' of the 'add more' button.
             unset($values['add_more']);
             // The original delta, before drag-and-drop reordering, is needed to
             // route errors to the correct form element.
             foreach ($values as $delta => &$value) {
                 $value['_original_delta'] = $delta;
             }
             usort($values, function ($a, $b) {
                 return SortArray::sortByKeyInt($a, $b, '_weight');
             });
         }
         // Let the widget massage the submitted values.
         $values = $this->massageFormValues($values, $form, $form_state);
         // Assign the values and remove the empty ones.
         $items->setValue($values);
         $items->filterEmptyItems();
         // Put delta mapping in $form_state, so that flagErrors() can use it.
         $field_state = static::getWidgetState($form['#parents'], $field_name, $form_state);
         foreach ($items as $delta => $item) {
             $field_state['original_deltas'][$delta] = isset($item->_original_delta) ? $item->_original_delta : $delta;
             unset($item->_original_delta, $item->_weight);
         }
         static::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
     }
 }
Ejemplo n.º 4
0
 /**
  * Tests SortArray::sortByTitleProperty() input against expected output.
  *
  * @dataProvider providerSortByTitleProperty
  * @covers ::sortByTitleProperty
  * @covers ::sortByKeyString
  *
  * @param array $a
  *   The first input item for comparison.
  * @param array $b
  *   The second item for comparison.
  * @param integer $expected
  *   The expected output from calling the method.
  */
 public function testSortByTitleProperty($a, $b, $expected)
 {
     $result = SortArray::sortByTitleProperty($a, $b);
     $this->assertBothNegativePositiveOrZero($expected, $result);
 }
 /**
  * {@inheritdoc}
  */
 public function massageFormValues(array $values, array $form, FormStateInterface $form_state)
 {
     $items = array();
     // Convert form values to actual entity reference values.
     foreach ($values as $value) {
         $item = $value;
         if (isset($item['entity'])) {
             $item['target_id'] = $item['entity']->id();
             $items[] = $item;
         }
     }
     // Sort items by _weight.
     usort($items, function ($a, $b) {
         return SortArray::sortByKeyInt($a, $b, '_weight');
     });
     return $items;
 }
 /**
  * Tests SortArray::sortByTitleProperty() input against expected output.
  *
  * @dataProvider providerSortByTitleProperty
  *
  * @param array $a
  *   The first input item for comparison.
  * @param array $b
  *   The second item for comparison.
  * @param integer $expected
  *   The expected output from calling the method.
  *
  * @see \Drupal\Component\Utility\SortArray::sortByTitleProperty()
  * @see \Drupal\Tests\Component\Utility\SortArrayTest::SortByTitleProperty()
  */
 public function testSortByTitleProperty($a, $b, $expected)
 {
     $result = SortArray::sortByTitleProperty($a, $b);
     $this->assertEquals($expected, $result);
 }
Ejemplo n.º 7
0
 /**
  * Sorts the dependency graph by reverse weight and alphabetically.
  *
  * @param array $a
  *   First item for comparison. The compared items should be associative
  *   arrays that include a 'weight' and a 'component' key.
  * @param array $b
  *   Second item for comparison.
  *
  * @return int
  *   The comparison result for uasort().
  */
 public function sortGraph(array $a, array $b)
 {
     $weight_cmp = SortArray::sortByKeyInt($a, $b, 'weight') * -1;
     if ($weight_cmp === 0) {
         return SortArray::sortByKeyString($a, $b, 'component');
     }
     return $weight_cmp;
 }
Ejemplo n.º 8
0
 /**
  * Tests SortArray::sortByWeightAndTitleKey() input against expected output.
  *
  * @dataProvider providerTestSortByWeightAndTitleKey
  *
  * @param array $a
  *   The first input item for comparison.
  * @param array $b
  *   The second item for comparison.
  * @param integer $expected
  *   The expected output from calling the method.
  */
 public function testSortByWeightAndTitleKey($a, $b, $expected)
 {
     $result = SortArray::sortByWeightAndTitleKey($a, $b);
     $this->assertEquals($expected, $result);
 }
Ejemplo n.º 9
0
 /**
  * Sort language objects.
  *
  * @param array $languages
  *   The array of language objects keyed by langcode.
  */
 public static function sort(&$languages)
 {
     uasort($languages, function ($a, $b) {
         return SortArray::sortByWeightAndTitleKey($a, $b, 'weight', 'name');
     });
 }
 /**
  * {@inheritdoc}
  */
 public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)
 {
     $field_name = $this->fieldDefinition->getName();
     // Extract the values from $form_state->getValues().
     $path = array_merge($form['#parents'], array($field_name));
     $key_exists = NULL;
     $values = NestedArray::getValue($form_state->getValues(), $path, $key_exists);
     if ($key_exists) {
         // Account for drag-and-drop reordering if needed.
         if (!$this->handlesMultipleValues()) {
             // Remove the 'value' of the 'add more' button.
             unset($values['add_more']);
             // The original delta, before drag-and-drop reordering, is needed to
             // route errors to the correct form element.
             foreach ($values as $delta => &$value) {
                 $value['_original_delta'] = $delta;
             }
             usort($values, function ($a, $b) {
                 return SortArray::sortByKeyInt($a, $b, '_weight');
             });
         }
         // Let the widget massage the submitted values.
         foreach ($values as $delta => &$value) {
             if (!empty($value['value']) && empty($value['fids'])) {
                 // ready to save the file
                 $provider_manager = \Drupal::service('video.provider_manager');
                 $allowed_providers = $this->getSetting('allowed_providers');
                 $enabled_providers = $provider_manager->loadDefinitionsFromOptionList($allowed_providers);
                 if ($provider_matches = $provider_manager->loadApplicableDefinitionMatches($enabled_providers, $value['value'])) {
                     $definition = $provider_matches['definition'];
                     $matches = $provider_matches['matches'];
                     $uri = $definition['stream_wrapper'] . '://' . $matches['id'];
                     $storage = \Drupal::entityManager()->getStorage('file');
                     $results = $storage->getQuery()->condition('uri', $uri)->execute();
                     if (!(count($results) > 0)) {
                         $user = \Drupal::currentUser();
                         $file = File::Create(['uri' => $uri, 'filemime' => $definition['mimetype'], 'filesize' => 1, 'uid' => $user->id()]);
                         $file->save();
                         unset($values[$delta]);
                         $values[] = array('fids' => array($file->id()), 'data' => serialize($matches));
                     } else {
                         unset($values[$delta]);
                         $values[] = array('fids' => array(reset($results)), 'data' => serialize($matches));
                     }
                 }
             }
         }
         $values = $this->massageFormValues($values, $form, $form_state);
         // Assign the values and remove the empty ones.
         $items->setValue($values);
         $items->filterEmptyItems();
         // Put delta mapping in $form_state, so that flagErrors() can use it.
         $field_state = static::getWidgetState($form['#parents'], $field_name, $form_state);
         foreach ($items as $delta => $item) {
             $field_state['original_deltas'][$delta] = isset($item->_original_delta) ? $item->_original_delta : $delta;
             unset($item->_original_delta, $item->_weight);
         }
         static::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
     }
 }
Ejemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function massageFormValues(array $values, array $form, FormStateInterface $form_state)
 {
     $ids = empty($values['target_id']) ? [] : explode(' ', trim($values['target_id']));
     $return = [];
     foreach ($ids as $id) {
         $item_values = ['target_id' => $id, '_weight' => $values['current'][$id]['_weight']];
         if ($this->fieldDefinition->getType() == 'file' && isset($values['current'][$id]['meta']['description'])) {
             $item_values['description'] = $values['current'][$id]['meta']['description'];
         }
         if ($this->fieldDefinition->getType() == 'image' && isset($values['current'][$id]['meta']['alt'])) {
             $item_values['alt'] = $values['current'][$id]['meta']['alt'];
         }
         if ($this->fieldDefinition->getType() == 'image' && isset($values['current'][$id]['meta']['title'])) {
             $item_values['title'] = $values['current'][$id]['meta']['title'];
         }
         $return[] = $item_values;
     }
     // Return ourself as the structure doesn't match the default.
     usort($return, function ($a, $b) {
         return SortArray::sortByKeyInt($a, $b, '_weight');
     });
     return array_values($return);
 }