class RealisticDummyContentTermReferenceField in Realistic Dummy Content 8.2
Same name and namespace in other branches
- 7.2 api/src/includes/RealisticDummyContentTermReferenceField.php \Drupal\realistic_dummy_content_api\includes\RealisticDummyContentTermReferenceField
- 3.x api/src/includes/RealisticDummyContentTermReferenceField.php \Drupal\realistic_dummy_content_api\includes\RealisticDummyContentTermReferenceField
Represents a term reference field.
Hierarchy
- class \Drupal\realistic_dummy_content_api\includes\RealisticDummyContentAttribute
- class \Drupal\realistic_dummy_content_api\includes\RealisticDummyContentField
- class \Drupal\realistic_dummy_content_api\includes\RealisticDummyContentTermReferenceField uses RealisticDummyContentDrupalTrait
- class \Drupal\realistic_dummy_content_api\includes\RealisticDummyContentField
Expanded class hierarchy of RealisticDummyContentTermReferenceField
1 file declares its use of RealisticDummyContentTermReferenceField
- RealisticDummyContentTermReferenceFieldTest.php in api/
src/ test/ includes/ RealisticDummyContentTermReferenceFieldTest.php
File
- api/
src/ includes/ RealisticDummyContentTermReferenceField.php, line 11
Namespace
Drupal\realistic_dummy_content_api\includesView source
class RealisticDummyContentTermReferenceField extends RealisticDummyContentField {
use RealisticDummyContentDrupalTrait;
/**
* {@inheritdoc}
*/
public function implementValueFromFile($file) {
try {
$termname = $file
->value();
if ($termname) {
$return = Framework::instance()
->formatProperty('tid', $this
->getTid($termname));
return $return;
}
} catch (\Exception $e) {
Framework::instance()
->debug('Problem with taxonomy term: ' . $e
->getMessage());
return NULL;
}
}
/**
* Returns term id for a term which is either existing or created on the fly.
*
* Let's say an entity (node) contains a term reference to the taxonomy
* vocabulary "location", and in the realistic dummy content file structure,
* "Australia" is used for the location. If "Australia" exists as a
* "location", then this function will return its tid. If not, the term will
* be created, and then the tid will be returned.
*
* If two terms have the same name, the tid of the first will be returned.
*
* @param string $name
* The string for the taxonomy term.
*
* @return int
* The associated pre-existing or just-created tid of the first term
* with the desired name.
*
* @throws \Exception
*/
public function getTid($name) {
$vocabularies = $this
->getAllVocabularies();
$field_info = $this
->fieldInfoField($this
->getName());
$candidate_existing_terms = array();
foreach ($field_info['settings']['allowed_values'] as $setting) {
$vocabulary_name = $setting['vocabulary'];
foreach ($vocabularies as $vocabulary) {
if ($this
->vocabularyMachineName($vocabulary) == $vocabulary_name) {
$candidate_existing_terms = array_merge($candidate_existing_terms, $this
->taxonomyLoadTree($vocabulary));
break 2;
}
}
}
foreach ($candidate_existing_terms as $candidate_existing_term) {
$candidate_name = $this
->termName($candidate_existing_term);
if ($candidate_name == $name) {
return $this
->termId($candidate_existing_term);
}
}
if (!isset($vocabulary)) {
throw new \Exception('Expecting the taxonomy term reference to reference at least one vocabulary');
}
$term = $this
->newVocabularyTerm($vocabulary, $name);
return $this
->termId($term);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RealisticDummyContentAttribute:: |
private | property | Entity managed by this class. | |
RealisticDummyContentAttribute:: |
private | property | The name of this attribuet, for example title, picture, field_image... | |
RealisticDummyContentAttribute:: |
public | function | Changes this attribute by looking for data in files. | |
RealisticDummyContentAttribute:: |
public | function | Given candidate files, change value of this attribute based on one of them. | |
RealisticDummyContentAttribute:: |
public | function | Returns the appropriate environment, real or testing. | |
RealisticDummyContentAttribute:: |
public | function | Return a file object. | |
RealisticDummyContentAttribute:: |
public | function | Gets the bundle of the associated entity. | |
RealisticDummyContentAttribute:: |
public | function | Get all candidate files for a given field for this entity. | |
RealisticDummyContentAttribute:: |
public | function | Getter for $this->entity. | |
RealisticDummyContentAttribute:: |
public | function | Get the entity type of the associated entity. | |
RealisticDummyContentAttribute:: |
public | function | Get acceptable file extensions which contain data for this attribute. | 2 |
RealisticDummyContentAttribute:: |
public | function | Return acceptable image file extensions. | |
RealisticDummyContentAttribute:: |
public | function | Getter for $this->name. | |
RealisticDummyContentAttribute:: |
public | function | Return acceptable text file extensions. | |
RealisticDummyContentAttribute:: |
public | function | Gets the UID of the associated entity. | |
RealisticDummyContentAttribute:: |
public | function | Return an image file object if possible. | |
RealisticDummyContentAttribute:: |
public | function | Returns a pseudo-random number. | |
RealisticDummyContentAttribute:: |
public | function | Given a RealisticDummyContentFileGroup object, get structured property. | |
RealisticDummyContentAttribute:: |
public | function | Given a list of files, return a value from one of them. | |
RealisticDummyContentAttribute:: |
public | function | Constructor. | |
RealisticDummyContentDrupalTrait:: |
public | function | Mockable wrapper around Framework::fieldInfoField(). | |
RealisticDummyContentDrupalTrait:: |
public | function | Mockable wrapper around Framework::getAllVocabularies(). | |
RealisticDummyContentDrupalTrait:: |
public | function | Mockable wrapper around Framework::newVocabularyTerm(). | |
RealisticDummyContentDrupalTrait:: |
public | function | Mockable wrapper around Framework::taxonomyLoadTree(). | |
RealisticDummyContentDrupalTrait:: |
public | function | Mockable wrapper around Framework::termId(). | |
RealisticDummyContentDrupalTrait:: |
public | function | Mockable wrapper around Framework::termName(). | |
RealisticDummyContentDrupalTrait:: |
public | function | Mockable wrapper around Framework::vocabularyMachineName(). | |
RealisticDummyContentField:: |
public | function |
Returns the type of this attribute. Overrides RealisticDummyContentAttribute:: |
|
RealisticDummyContentTermReferenceField:: |
public | function | Returns term id for a term which is either existing or created on the fly. | |
RealisticDummyContentTermReferenceField:: |
public | function |
Given a RealisticDummyContentFileGroup object, get a structured property. Overrides RealisticDummyContentAttribute:: |