foldershare-8.x-1.2/src/Entity/FolderShareTraits/GetSetFileTrait.php
src/Entity/FolderShareTraits/GetSetFileTrait.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | <?php namespace Drupal\foldershare\Entity\FolderShareTraits; use Drupal\file\Entity\File; use Drupal\media\Entity\Media; /** * Get/set FolderShare entity file, image, and media fields. * * This trait includes get methods for FolderShare entity file, image, * and media fields. Each field is an entity reference that stores * a target entity ID. At most one of these three fields may be set. * * <B>Internal trait</B> * This trait is internal to the FolderShare module and used to define * features of the FolderShare entity class. It is a mechanism to group * functionality to improve code management. * * @ingroup foldershare */ trait GetSetFileTrait { /*--------------------------------------------------------------------- * * File field. * *---------------------------------------------------------------------*/ /** * Clears the item's file ID. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * The caller must call save() for the change to take effect. * * <B>Process locks</B> * This method does not lock access. The caller should lock around changes * to the entity. * * @see ::setFileId() */ private function clearFileId() { $this ->file->setValue([ 'target_id' => NULL]); } /** * {@inheritdoc} */ public function getFile() { $id = $this ->getFileId(); if ( $id === FALSE) { return NULL; } return File::load( $id ); } /** * {@inheritdoc} */ public function getFileId() { if ( $this ->isFile() === TRUE) { $value = $this ->get( 'file' )->target_id; if ( $value === NULL) { return FALSE; } return (int) $value ; } return FALSE; } /** * Sets the item's file ID. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * The entity ID is not validated and is presumed to be a File entity ID. * This item is presumed to be a file kind (i.e. isFile() returns TRUE). * * The caller must call save() for the change to take effect. * * <B>Process locks</B> * This method does not lock access. The caller should lock around changes * to the entity. * * @param int $id * The new File entity ID. The value is not validated but is expected * to be a valid File entity ID. If the value is negative, the file ID * is cleared. * * @see ::clearFileId() */ private function setFileId(int $id ) { if ( $id < 0) { $this ->file->setValue([ 'target_id' => NULL]); } else { $this ->file->setValue([ 'target_id' => $id ]); } } /*--------------------------------------------------------------------- * * Image field. * *---------------------------------------------------------------------*/ /** * Clears the item's image ID. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * The caller must call save() for the change to take effect. * * <B>Process locks</B> * This method does not lock access. The caller should lock around changes * to the entity. * * @see ::setImageId() */ private function clearImageId() { $this ->image->setValue([ 'target_id' => NULL]); } /** * {@inheritdoc} */ public function getImage() { $id = $this ->getImageId(); if ( $id === FALSE) { return NULL; } // The image module does not define an Image entity type. // Instead it uses the File entity type, but references it // via an image field type. So, to load an image, we need // to use the File module. return File::load( $id ); } /** * {@inheritdoc} */ public function getImageId() { if ( $this ->isImage() === TRUE) { $value = $this ->get( 'image' )->target_id; if ( $value === NULL) { return FALSE; } return (int) $value ; } return FALSE; } /** * Sets the item's image ID. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * The entity ID is not validated and is presumed to be a File entity ID. * This item is presumed to be an image kind (i.e. isImage() returns TRUE). * * The caller must call save() for the change to take effect. * * <B>Process locks</B> * This method does not lock access. The caller should lock around changes * to the entity. * * @param int $id * The new File entity ID. The value is not validated but is expected * to be a valid File entity ID. If the value is negative, the image ID * is cleared. * * @see ::clearImageId() */ private function setImageId(int $id ) { if ( $id < 0) { $this ->image->setValue([ 'target_id' => NULL]); } else { $this ->image->setValue([ 'target_id' => $id ]); } } /*--------------------------------------------------------------------- * * Media field. * *---------------------------------------------------------------------*/ /** * Clears the item's media ID. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * The caller must call save() for the change to take effect. * * <B>Process locks</B> * This method does not lock access. The caller should lock around changes * to the entity. * * @see ::setMediaId() */ private function clearMediaId() { $this ->media->setValue([ 'target_id' => NULL]); } /** * {@inheritdoc} */ public function getMedia() { $id = $this ->getMediaId(); if ( $id === FALSE) { return NULL; } return Media::load( $id ); } /** * {@inheritdoc} */ public function getMediaId() { if ( $this ->isMedia() === TRUE) { $value = $this ->get( 'media' )->target_id; if ( $value === NULL) { return FALSE; } return (int) $value ; } return FALSE; } /** * Sets the item's media ID. * * <B>This method is internal and strictly for use by the FolderShare * module itself.</B> * * The entity ID is not validated and is presumed to be a Media entity ID. * This item is presumed to be a media kind (i.e. isMedia() returns TRUE). * * The caller must call save() for the change to take effect. * * <B>Process locks</B> * This method does not lock access. The caller should lock around changes * to the entity. * * @param int $id * The new Media entity ID. The value is not validated but is expected * to be a valid Media entity ID. If the vlaue is negative, the media ID * is cleared. * * @see ::clearMediaId() */ private function setMediaId(int $id ) { if ( $id < 0) { $this ->media->setValue([ 'target_id' => NULL]); } else { $this ->media->setValue([ 'target_id' => $id ]); } } } |