1 | <?php |
||
33 | class PanelizerThumbnailFormatter extends FormatterBase implements ContainerFactoryPluginInterface { |
||
34 | |||
35 | /** |
||
36 | * How deep this formatter should look for a file. |
||
37 | * |
||
38 | * @var int |
||
39 | */ |
||
40 | const DEPTH_LIMIT = 5; |
||
41 | |||
42 | /** |
||
43 | * @var \Drupal\Core\Entity\EntityStorageInterface |
||
44 | */ |
||
45 | protected $responsiveImageStyleStorage; |
||
46 | |||
47 | /** |
||
48 | * @var \Drupal\panelizer\PanelizerInterface |
||
49 | */ |
||
50 | protected $panelizer; |
||
51 | |||
52 | /** |
||
53 | * @var \Drupal\Core\Image\ImageFactory |
||
54 | */ |
||
55 | protected $imageFactory; |
||
56 | |||
57 | /** |
||
58 | * @var \Drupal\Core\Entity\EntityRepositoryInterface |
||
59 | */ |
||
60 | protected $entityRepository; |
||
61 | |||
62 | /** |
||
63 | * Constructs a PanelizerThumbnailFormatter object. |
||
64 | * |
||
65 | * @param string $plugin_id |
||
66 | * The plugin_id for the formatter. |
||
67 | * @param mixed $plugin_definition |
||
68 | * The plugin implementation definition. |
||
69 | * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition |
||
70 | * The definition of the field to which the formatter is associated. |
||
71 | * @param array $settings |
||
72 | * The formatter settings. |
||
73 | * @param string $label |
||
74 | * The formatter label display setting. |
||
75 | * @param string $view_mode |
||
76 | * The view mode. |
||
77 | * @param array $third_party_settings |
||
78 | * Any third party settings. |
||
79 | * @param \Drupal\Core\Entity\EntityStorageInterface $responsive_image_style_storage |
||
80 | * The responsive image style storage. |
||
81 | * @param \Drupal\panelizer\PanelizerInterface $panelizer |
||
82 | * The panelizer service. |
||
83 | */ |
||
84 | public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityStorageInterface $responsive_image_style_storage, PanelizerInterface $panelizer, ImageFactory $image_factory, EntityRepositoryInterface $entity_repository) { |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public static function defaultSettings() { |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | public function settingsSummary() { |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | public function settingsForm(array $form, FormStateInterface $form_state) { |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | public function viewElements(FieldItemListInterface $items, $langcode) { |
||
209 | |||
210 | /** |
||
211 | * Finds the first file from a given Panelized entity. |
||
212 | * |
||
213 | * @param \Drupal\Core\Entity\EntityInterface|FieldableEntityInterface $entity |
||
214 | * A Panelized entity. |
||
215 | * @return \Drupal\file\FileInterface|bool |
||
216 | * The first file displayed in a given display, or FALSE if none was found. |
||
217 | */ |
||
218 | protected function getFileFromPanelizedEntity($entity) { |
||
245 | |||
246 | /** |
||
247 | * Gets the first file present in a given entity. |
||
248 | * |
||
249 | * @param \Drupal\Core\Entity\EntityInterface|FieldableEntityInterface $entity |
||
250 | * The entity you want to find a file for. |
||
251 | * @return \Drupal\file\FileInterface|bool |
||
252 | * The first file found for a given entity, or FALSE |
||
253 | */ |
||
254 | protected function getFileFromEntity(FieldableEntityInterface $entity) { |
||
263 | |||
264 | /** |
||
265 | * Gets the first file from a given field. |
||
266 | * |
||
267 | * If the field is a Media reference, recursion is used to find nested |
||
268 | * file fields. |
||
269 | * |
||
270 | * @param \Drupal\Core\Field\FieldItemListInterface $field |
||
271 | * @return \Drupal\file\FileInterface|bool |
||
272 | */ |
||
273 | protected function getFileFromField(FieldItemListInterface $field) { |
||
295 | |||
296 | } |
||
297 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.